6: Classes and objects 类和对象
Variables stores values of a certain type. The best way to refer to that is with object. The number 10 is an object. If you assign 10 to a variable (e.g. my_var = 10), the variable will have the object of 10. The type of the object is the same as the value’s type (data type), we’ll just be referring to it differently.
变量可以存储某一种类型的数据,在Ruby中,任何数据都是对象,所以数据的类型也就是对象的类型。
数字10是一个对象,如果你写my_var = 10,那么my_var这个变量就指向了对象10。
A variable stores an object for when you need to use it multiple times, such as this:
当你用一个变量存储了一个对象时,你就可以随时使用这个对象了。
But an object doesn’t have to be associated with a variable.
但是,对象并不一定要和变量相关联,请看下面的例子:
There are three objects in this example:
0
79
[79,80]
在上面的例子中,总共有3个对象,分别是0、79和[79, 80]。
79这个对象是通过[79, 80][gender]计算得出的。
Out of these, 0 and 79 can be reused because they’re associated with a variable (gender and life_expectancy). The array [79,80] is still an object and a newly created array, but once the line life_expectancy = [79,80][gender] is done, [79,80] is no longer accessible. The object is disposed by the garbage collector (which is just something that deletes old unreachable objects/code to lower RAM usage), because there’s no way to access [79,80], as it isn’t a variable.
在上面的例子中,0和79可以随时使用,因为它们和变量关联在了一起,我们可以通过变量随时访问对应的对象。
但是,对于[79, 80]这个对象,在life_expectancy = [79, 80][gender]这一行运行结束之后,我们将不再能访问它,因为这个对象并没有指向任何一个变量,所以在life_expectancy = [79, 80][gender]这一行运行结束之后,[79, 80]这个对象就会被程序清除,用以节省内存。
当你看到我写“指向了”、“存储了”、“保存了”、“赋予了”、“关联了”,等等的词时,它们都是一个意思,就是指的那个对象是否有变量和它相关联,我们是否可以通过变量访问那个对象。
Variables stores values of a certain type. The best way to refer to that is with object. The number 10 is an object. If you assign 10 to a variable (e.g. my_var = 10), the variable will have the object of 10. The type of the object is the same as the value’s type (data type), we’ll just be referring to it differently.
变量可以存储某一种类型的数据,在Ruby中,任何数据都是对象,所以数据的类型也就是对象的类型。
数字10是一个对象,如果你写my_var = 10,那么my_var这个变量就指向了对象10。
A variable stores an object for when you need to use it multiple times, such as this:
当你用一个变量存储了一个对象时,你就可以随时使用这个对象了。
Ruby:
gender = 0
# 0是一个对象,gender存储了这个对象。
But an object doesn’t have to be associated with a variable.
但是,对象并不一定要和变量相关联,请看下面的例子:
Ruby:
gender = 0
life_expectancy = [79,80][gender]
There are three objects in this example:
0
79
[79,80]
在上面的例子中,总共有3个对象,分别是0、79和[79, 80]。
79这个对象是通过[79, 80][gender]计算得出的。
Out of these, 0 and 79 can be reused because they’re associated with a variable (gender and life_expectancy). The array [79,80] is still an object and a newly created array, but once the line life_expectancy = [79,80][gender] is done, [79,80] is no longer accessible. The object is disposed by the garbage collector (which is just something that deletes old unreachable objects/code to lower RAM usage), because there’s no way to access [79,80], as it isn’t a variable.
在上面的例子中,0和79可以随时使用,因为它们和变量关联在了一起,我们可以通过变量随时访问对应的对象。
但是,对于[79, 80]这个对象,在life_expectancy = [79, 80][gender]这一行运行结束之后,我们将不再能访问它,因为这个对象并没有指向任何一个变量,所以在life_expectancy = [79, 80][gender]这一行运行结束之后,[79, 80]这个对象就会被程序清除,用以节省内存。
当你看到我写“指向了”、“存储了”、“保存了”、“赋予了”、“关联了”,等等的词时,它们都是一个意思,就是指的那个对象是否有变量和它相关联,我们是否可以通过变量访问那个对象。