【发布时间】:2011-07-31 17:59:13
【问题描述】:
这个问题完全是关于 Struct 行为的,所以请不要“在广阔的体育世界中,你为什么要那样做?”
这段代码不正确,但它应该说明我想了解的关于 Ruby Structs 的内容:
class Person < Struct.new(:name, :last_name)
end
class ReligiousPerson < Person(:religion)
end
class PoliticalPerson < Person(:political_affiliation)
end
### Main ###
person = Person.new('jackie', 'jack')
pious_person = ReligiousPerson.new('billy', 'bill', 'Zoroastrianism')
political_person = PoliticalPerson.new('frankie', 'frank', 'Connecticut for Lieberman')
如您所见,有人尝试使用 Structs 定义类继承。但是,当您尝试初始化 ReligiousPerson 或 PoliticalPerson 时,Ruby 会变得暴躁,当然。那么鉴于这个说明性代码,如何使用这种类型的类继承来继承命名参数呢?
【问题讨论】:
标签: ruby inheritance struct