我很高兴知道为什么"Cat"
下面的实例也没有将"This animal can:"
文本放在其特定实例属性之前。我期待这样的输出:
This animal can:
Say it's name: 'Rover'
Bark
This animal can:
Say its name: 'Satan'
Meow
这是代码:
class Animal
puts "This animal can:"
end
class Dog < Animal
def initialize(name)
@name = name
puts "Say its @name: '%s'" % [name]
end
def bark
puts "Bark"
end
end
class Cat < Animal
def initialize(name)
@name = name
puts "Say its @name: '%s'" % [name]
end
def meow
puts "Meow"
end
end
rover = Dog.new("Rover").bark
satan = Cat.new("Satan").meow
我看到的是这样的:
This animal can:
Say it's name: 'Rover'
Bark
Say its name: 'Satan'
Meow
不"cat"
也继承自Animal
类吗?它的输出不应该也以 开头"This animal can:"
吗?