实例变量

Instance variables

实例变量

An instance variable has a name beginning with @, and its scope is confined to whatever object self refers to.

一个实例变量的名称以@开头,而且它的作用域仅限于该对象自身。

Two different objects, even if they belong to the same class, are allowed to have different values for their instance variables.

2个不同的对象,尽管它们属于同一个类,允许对实例变量有不同的值。

From outside the object, instance variables cannot be altered or even observed (i.e., ruby’s instance variables are never public) except by whatever methods are explicitly provided by the programmer.

对于外部对象,除了由程序员明确提供的方法之外,实例变量不可改变,甚至不可见(即,Ruby的实例变量从来不是公开的)。

As with globals, instance variables have the nil value until they are initialized.

全局变量一样,实例变量在初始化之前的值为nil

Instance variables do not need to be declared.

实例变量不需要声明。

This indicates a flexible object structure; in fact, each instance variable is dynamically appended to an object when it is first assigned.

这表明了一个灵活的对象结构,事实上,每个实例变量都是在第一次被赋值时被动态地追加到一个对象中。

  1. ruby> class InstTest
  2. | def set_foo(n)
  3. | @foo = n
  4. | end
  5. | def set_bar(n)
  6. | @bar = n
  7. | end
  8. | end
  9. nil
  10. ruby> i = InstTest.new
  11. #<InstTest:0x83678>
  12. ruby> i.set_foo(2)
  13. 2
  14. ruby> i
  15. #<InstTest:0x83678 @foo=2>
  16. ruby> i.set_bar(4)
  17. 4
  18. ruby> i
  19. #<InstTest:0x83678 @foo=2, @bar=4>

Notice above that i does not report a value for @bar until after the set_bar method is invoked.

注意,上面的i在调用set-bar方法之前并没有返回@bar的值。

上一章 全局变量
下一章 局部变量