尝试学习/理解 Ruby 的 setter 和 getter 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8737421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to learn / understand Ruby setter and getter methods
提问by Nathan
I'm just learning to program and have decided to try Ruby. I'm sure this is a stupid question, but the instructor is talking about setter and getter methods, and I'm confused. Here is the example:
我刚刚学习编程并决定尝试 Ruby。我确定这是一个愚蠢的问题,但讲师正在谈论 setter 和 getter 方法,我很困惑。这是示例:
class Human
def noise=(noise)
@noise = noise
end
def noise
@noise
end
end
From this, the class is instantiated, and I can puts this out:
由此,类被实例化,我可以把它放出来:
man = Human.new
man.noise=("Howdie!")
puts man.noise
This results in Howdie!
这导致 Howdie!
Now what confuses me is that the instructor is saying without the getter method (the 2nd of the two methods), there is no way to interact with the instance variable @noise.
现在让我感到困惑的是,讲师说没有 getter 方法(两种方法中的第二种),就无法与实例变量 @noise 进行交互。
But when I remove the getter method, I'm able to still access @noise, see example:
但是当我删除 getter 方法时,我仍然可以访问 @noise,请参见示例:
class Human
def noise=(noise)
@noise = noise
end
end
man = Human.new
puts man.noise=("Howdie!")
This works the same as when the getter method it used.
这与它使用的 getter 方法相同。
So now I'm confused. Why is the getter needed? What does the instructor mean by not being able to access the instance variable without it? Is it possible he's using an older version of Ruby?
所以现在我很困惑。为什么需要吸气剂?没有它就无法访问实例变量是什么意思?他有可能使用旧版本的 Ruby 吗?
Thanks in advance for your help.
在此先感谢您的帮助。
采纳答案by Ryan Bigg
You can interact with that instance variable from other methods belonging to that instance, even if there is no getter:
即使没有 getter,您也可以从属于该实例的其他方法与该实例变量进行交互:
def noise=(noise)
@noise = noise
end
def last_noise
@noise
end
There doesn't need to be a getter defined with the same name as the method; the two are not linked at all. The getter is needed to "get" the value of the instance variable, but only in a shortsyntax.
不需要定义与方法同名的 getter;两者根本没有联系。需要 getter 来“获取”实例变量的值,但只需要使用简短的语法。
What's happening in your example is that you're initializing a new object (Human.new), and then using a method (noise=, yes the method name contains the =symbol) that just-so-happens to define an instance variable (that is, a variable justfor that instance), and then finally retrieving that instance variable with anothermethod call.
在您的示例中发生的事情是您正在初始化一个新对象 ( Human.new),然后使用一个方法 ( noise=,是的,方法名称包含=符号) 恰好定义了一个实例变量 (即,一个变量只是对于那个实例),然后最后用另一个方法调用检索该实例变量。
You can actually use instance_variable_getto get the instance variable without defining anygetter at all:
您实际上instance_variable_get可以在不定义任何getter的情况下使用来获取实例变量:
man = Human.new
man.noise = "Howdie"
man.instance_variable_get("@noise")
This will return "Howdie", even though there is no getter defined.
这将返回“Howdie”,即使没有定义 getter。
And no, I don't think he's using an older version of Ruby.
不,我不认为他在使用旧版本的 Ruby。
回答by David Grayson
The line of code
代码行
puts man.noise=("Howdie!")
does NOT use the getter method, so the getter method does not need to be defined for it to work. That line just uses the setter method. The return value of the setter method is automatically equal to whatever is on the right-hand side of the equal sign, so "Howdie!" gets passed to puts.
不使用 getter 方法,因此不需要定义 getter 方法即可工作。该行仅使用 setter 方法。setter 方法的返回值自动等于等号右侧的任何值,所以“Howdie!” 传递给puts.
The line of code
代码行
puts man.noise
DOES use the getter method, and it would not work if you remove the getter method.
确实使用 getter 方法,如果您删除 getter 方法,它将不起作用。
回答by Jokester
Surely they both return a value, but their behavior are different.
当然它们都返回一个值,但它们的行为是不同的。
Let's say there is already a member @a.
假设已经有成员了@a。
with getter, one obtains the current value of @a, without modifying it.
使用 getter,可以获取 的当前值@a,而无需对其进行修改。
with setter, one modifies @a, and get its new value as return value.
使用 setter,修改@a,并获取其新值作为返回值。
when thinking about behavior of setter, note:
在考虑 setter 的行为时,请注意:
the old value of @a can not be obtained with setter, and got overwritten.
what returned by setter, is actually already known before invoking setter.
@a 的旧值无法使用 setter 获得,并被覆盖。
setter 返回的是什么,其实在调用setter 之前就已经知道了。
回答by steenslag
May be the attention for getters and setters is because some other languages allow you to access class variables directly. Python:
对 getter 和 setter 的关注可能是因为其他一些语言允许您直接访问类变量。Python:
class k:
i = 100
print k.i # 100
k.i = 200
print k.i # 200
In contrast, Ruby keeps all of its variables completely private to the class and only exposes them through accessor methods.
相比之下,Ruby 将其所有变量对类完全私有,并且仅通过访问器方法公开它们。
In your example, if you remove the getter, you can indeed interact with the variable (that is: change it) trough the setter, but you can't get it back (in a regular way) when you need it.
在您的示例中,如果您删除了 getter,您确实可以通过 setter 与变量交互(即:更改它),但是您无法在需要时(以常规方式)取回它。

