Ruby class instance variable, class instance method and class variable, class method difference _lua

Source: Internet
Author: User
Tags class definition instance method

The distinction between class instance variables, class instance methods, and class variables, class methods, and classes in Ruby is subtle, and the usage is quite different. This article discusses their definition and basic use of the scene to ...

I. Class instance variables and class variables

Class variables are familiar to us, and are variables that start with @@ 开头 the class definition. A class variable is the global information that is used to store the class, and it belongs to the class only, and differs from the class instance variable (that is, the variable defined at the beginning of @) for each class object. Class variables can be inherited, that is, if we derive a subclass, we can access the class variable of the parent class in the subclass. Subclasses and parent classes share a single piece of data, and modifications to one class are reflected in another class. As the following code runs, the results show:

Copy Code code as follows:

Class A
#类变量在访问前必须赋值, otherwise there will be "uninitialized class variable" error
@ @alpha =123 # Initialize @ @alpha
@ @beta =456 #Initialize @ @beta
@ @gamma =789 #Initialize @ @gamma

def A.alpha
@ @alpha
End

def a.alpha= (x)
@ @alpha =x
End

def A.beta
@ @beta
End

def a.beta= (x)
@ @beta =x
End

def A.gamma
@ @gamma
End

def a.gamma= (x)
@ @gamma =x
End
def A.look
Puts "#@ @alpha, #@ @beta, #@ @gamma"
End
End


Class B<a
End

#初始的数据
A.look
B.look

#修改父类中的类变量
a.alpha=111
A.look
B.look


Run Result:
Copy Code code as follows:

123, 456, 789
123, 456, 789
111, 456, 789
111, 456, 789

So what are class instance variables for classes? Class instance variables of classes are variables that are defined in the class method of a class or outside of a method (not in an instance method, which is an instance variable of a class), and are defined with the value of the class object itself and cannot be inherited by the quilt class. Class object This argument may be somewhat confusing, in fact Ruby is an object, we define the class and Ruby's built-in class itself is the object of the class. So if you want to define a class global data that needs to be shared with a subclass, use a class variable, but you can use a class instance variable if you want to define a class global data that is used only by the class itself. It is also important to note that unlike class variables, class instance variables are not required to be assigned a value before access, and the value is nil. Let's look at an example to make it clearer ...

Copy Code code as follows:

Class A
#类的类实例变量在访问前可以赋值也可以不赋值, no value is nil
@alpha =123 # Initialize @alpha
@beta =456 #Initialize @beta
@gamma =789 #Initialize @gamma

def A.alpha
@alpha
End

def a.alpha= (x)
@alpha =x
End

def A.beta
@beta
End

def a.beta= (x)
@beta =x
End

def A.gamma
@gamma
End

def a.gamma= (x)
@gamma =x
End
def A.look
Puts "# @alpha, # @beta, # @gamma"
End
End


Class B<a
End

A.look
B.look

The code runs as follows:
Copy Code code as follows:

123, 456, 789
, ,
Nil

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.