An instance variable (x
) is a variable that is local to an instance. Neither the class nor any other instance can access that variable.
A class variable (Yy
) is local to a class, all its instances, all subclasses and all subinstances (so the entire hierarchy). Any subclass or subinstance can see the value of that variable.
A class instance variable (zzz
) is local to a class. Only the class that defines the variable has access to it, neither instances nor subclasses can see the variable (although subclasses inherit the declaration of the variable, their variable will have a different value).
Classes are also objects in Smalltalk. So you can think of a class instance variable the same way you would think about an instance variable: no other instance (instance of a class) can see the value. Thanks to @Amos M. Carpenter for pointing this out.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…