Instance variables 实例变量
As the name suggests, instance variables are variables bound to class instances and its scope. Whereas local variables exist in one scope only, instance variables are carried by the instance/object of the class. The difference between local variables and instance variables is that the latter always start with 1 ‘@’. Whether or not the letter after that is uppercase or lowercase doesn’t matter.
实例变量,顾名思义,就是专用于实例的变量。
实例变量用来存储实例的某一个属性,或者说状态,我们来看下面的这个例子:
If you were to try this with local variables (without the @), it would perform age = 10 in def initialize, but then that method ends and the variable is gone again. Then calling print_age would try to access a non-existent variable, because there’s no local variable defined in print_age called age.
在上面的例子中,如果你不使用实例变量,而是使用局部变量的话,也就是age = 10,那么在初始化方法结束之后,这个变量没有了,你无法在print_age中调用age。
你也可以用作用域来理解,初始化方法的作用域和print_age方法的作用域是独立不互通的,所以它们之间的局部变量不能互相访问,而实例变量就可以在同一个类中的不同方法之间直接访问。
This is solved by using instance variables. These variables exist as long as the object they’re in exists. We can’t directly access this variable from outside of the class though, nor on the object itself (var). You have to be in the class to manipulate instance variables. For this you need to create helper methods, methods that allow you to read and write to the variable from the outside. This is pretty basic:
实例变量虽然可以在同一个类中的不同方法之间直接访问,但是却不能在这个类之外直接进行访问,你只能在这个类之内操纵实例变量。
为了能在外部访问实例变量,我们需要创建一些辅助方法来帮助我们,请看下面的这个例子:
But that’s convoluted and annoying to do for every single instance variable (if it’s a big class, you can easily have dozens of instance variables). That’s why we can create getter and setter methods. Getter methods are basically just methods with the same name as the instance variable, which return the value of said instance variable (exactly the same as get_age but with a different name). Setters are a bit different though. Rather than set_age(10), setter methods allow you to use assignment with = to call the method (yes, assignment is also a method!)
但是,为每一个实例变量都添加这样的两个方法是非常烦人的。
所以我们创造了获取器和设置器方法。
获取器是一个和变量的名字相同的方法,而设置器是一个在变量的名字后面加一个“=”的名字的方法。
假如说有个实例变量是@age,那获取器就是age,设置器就是age=,注意,这个“=”是设置器名字的一部分。
First to demonstrate the getter method:
首先我们来看获取器的例子:
Next comes the setter method:
接着我们来看设置器的例子:
So when you create a method with an equal sign = at the end of the method name, that method will be treated as an assignment method, with =.
There’s really no need for getter and setter methods, unless you want the instance variable to BE public and to be used outside of the class itself.
These getter and setter methods can be very useful, but it’s a lot to write if you have a lot of instance variables. That’s why there are shortcuts for these getters and setters:
attr_reader: This is the getter method. Equal to def age in the previous examples.
attr_writer: This is the setter method. Equal to def age=(v) in the previous examples.
attr_accessor: This is both the getter and the setter method. Equal to def age and def age=(v) in the previous examples.
因为获取器和设置器实在是太基础也太重要了,所以我们又创造了更加简便的添加获取器和设置器的方法。
添加获取器,我们只需要写attr_reader(读取器),添加设置器,我们只需要写attr_writer(写入器),而如果你写attr_accessor(访问器)的话,就是同时添加了获取器和设置器。
attr also means Attribute. Attribute reader (getter), Attribute writer (setter), Attribute accessor (getter + setter).
这里的“attr”其实是attribute(属性)的简写,attr_reader就是属性的读取器,attr_writer就是属性的写入器,attr_accessor就是属性的访问器,即读取器+写入器。
To use these shortcuts, you choose the keyword followed by a colon :, followed by the name of the instance variable. When used, it will basically turn into the proper method(s) in the order you wrote them. This means that you can overwrite the functionality later, if you want, just by typing out the method and implementing your desired functionality (just like a normal method).
那么具体该怎么做呢?
我们来看下面的这个例子: