在做 super 测试时,按照实例修改了一下,结果报错,代码如下:
class Person
attr_accessor :name ,:age ,:sex1
def say_hello
puts "#{self.name}"
end
end
class Man < Person
def say_sex1
puts "i sex is #{self.sex1}"
super
say_hello
end
end
man = Man.new
man.name="zhangsan"
#man.age= 20
man.sex1 = "Nan"
man.say_sex1
错误:
D:/rubytest/3.14:18:in `say_sex1': super: no superclass method `say_sex1' for #<Man:0x290a420 @name="zhangsan", @age=20, @sex1="Nan"> (NoMethodError)
from D:/rubytest/3.14:31:in `<main>'
i sex is Nan
[Finished in 0.1s with exit code 1]
为什么名字输出不了?