实例变量

    实例变量

    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.

    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.

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

    实例变量不需要声明。

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

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

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

    上一章 全局变量