0
A
是父类,B
和C
是它的子类。我可以确保B
和C
都有方法hello
通过在类A
中添加以下行。Ruby:我希望子类总是声明某些类变量
def hello
raise "Implement hello method in sub class"
end
我怎样才能acheive一类变量的A
每个子类都应该有相同的?
A
是父类,B
和C
是它的子类。我可以确保B
和C
都有方法hello
通过在类A
中添加以下行。Ruby:我希望子类总是声明某些类变量
def hello
raise "Implement hello method in sub class"
end
我怎样才能acheive一类变量的A
每个子类都应该有相同的?
据我理解你的问题(不知道你所谓的variable
),你可以简单地定义变量是这样的:
def my_variable
@my_variable || raise 'Not defined'
end
...而不是:
attr_accessor :my_variable # a 'true' instance variable
希望它可以帮助。