在我展开我的问题之前,让我声明我已经阅读了这里、这里和这里的问题的答案。因此,要求您不要将我的问题标记为与那些答案一样重复并没有让我理解attr_accessor
. 我的问题更多与逻辑有关,而不是语法。
我在下面创建了两组代码。这些集合彼此相同,只是其中一个集合根本没有attr_accessor
线。当我运行两组时,它们都给了我相同的输出。attr_accessor
那么,从逻辑上讲,当两组代码给我相同的预期输出时,这行代码有什么区别?
代码集 1:
class Animal
def initialize(name)
@name = name
end
end
class Cat < Animal
def talk
"Meaow!"
end
end
class Dog < Animal
def talk
"Woof!"
end
end
animals = [Cat.new("Flossie"), Dog.new("Clive"), Cat.new("Max")]
animals.each do |animal|
puts animal.talk
end
#Output:
#Meaow!
#Woof!
#Meaow!
代码集 2:
class Animal
attr_accessor :name #this line is the only difference between the two code sets.
def initialize(name)
@name = name
end
end
class Cat < Animal
def talk
"Meaow!"
end
end
class Dog < Animal
def talk
"Woof!"
end
end
animals = [Cat.new("Flossie"), Dog.new("Clive"), Cat.new("Max")]
animals.each do |animal|
puts animal.talk
end
#Output:
#Meaow!
#Woof!
#Meaow!
Both sets of code call the Animal class to create new instances of animal objects WITH names. I stress on "...WITH names." because the attr_accessor (in the 2nd set) is defining the :name
attribute. But in the 1st code set, I have deleted the attr_accessor
but still managed to create object instances with the name attribute.