I've always thought instance variable as used only within the definition of a class and can be accessed inside the class. When I am learning rails I am looking at an example like this:
我一直认为实例变量只在类的定义中使用,可以在类中访问。当我学习rails时,我正在看一个这样的例子:
class Movie
def initialize(isbn)
@isbn = isbn
end
def isbn
@isbn
end
end
@movie = Movie.new("0702")
@movie.isbn
I'm confused that a instance variable appears outside of a class definition. Why use @movie instead of local variable "movie"?
我很困惑,实例变量出现在类定义之外。为什么使用@movie而不是局部变量“movie”?
Follow up questions:
跟进问题:
-
Since instance variables can live in a another scope, when the program need to create multiple new objects, do they all have the same name @movie? Are the new object always overwirte the old one?
由于实例变量可以存在于另一个范围内,当程序需要创建多个新对象时,它们是否都具有相同的名称@movie?新物体是否总是超过旧物体?
-
In a larger program(like a MVC app), does the @movie shared by all the files(model, view, controller)?
在一个更大的程序(如MVC应用程序)中,@ movie是否由所有文件(模型,视图,控制器)共享?
3 个解决方案
#1
2
I've always thought instance variable as used only within the definition of a class and can be accessed inside the class.
我一直认为实例变量只在类的定义中使用,可以在类中访问。
That's wrong. Instance variables have nothing to do with classes at all. Instance variables belong to objects (aka instances), that's why they are called instance variables.
那是错的。实例变量与类完全无关。实例变量属于对象(也称为实例),这就是它们被称为实例变量的原因。
I'm confused that a instance variable appears outside of a class definition.
我很困惑,实例变量出现在类定义之外。
It is perfectly fine for an instance variable to appear outside of a class definition. After all, the other instance variables in your example are also not inside a class definition, they are in method definitions.
实例变量出现在类定义之外是完全正确的。毕竟,示例中的其他实例变量也不在类定义中,它们在方法定义中。
Why use @movie instead of local variable "movie"?
为什么使用@movie而不是局部变量“movie”?
There is no reason to do that in the code snippet you posted. But if it were part of a larger program, there certainly could be good reasons to do so.
在您发布的代码段中没有理由这样做。但如果它是一个更大的计划的一部分,那么肯定有充分的理由这样做。
#2
0
Instance variables have another scope than local variables. Outside a class you are still in the main Object class as you can see in the error produced in the following example. So in my example if I had to use the movie variable in a method like test I need an instance variable or (better) I would need to pass it as a parameter.
实例变量具有另一个范围而不是局部变量。在类之外,您仍然在主Object类中,您可以在以下示例中生成的错误中看到。所以在我的例子中,如果我必须在像测试这样的方法中使用movie变量,我需要一个实例变量或(更好)我需要将它作为参数传递。
In this case they are not much better than a global variable in poluting the namespace. Couldn't find a good article about Ruby and using variables and namespaces although I read them in the past. If I do, I'll add it to the answer.
在这种情况下,它们在规范名称空间时并不比全局变量好多少。虽然我过去读过它们但找不到关于Ruby和使用变量和命名空间的好文章。如果我这样做,我会把它添加到答案中。
class Movie
def initialize(isbn)
@isbn = isbn
end
def isbn
@isbn
end
end
@movie = Movie.new("0702")
@movie.isbn
movie = "you won't see me"
def test
p @movie # #<Movie:0x00000002d1e7f8 @isbn="0702">
p movie # error undefined local variable or method `movie' for main:Object
end
test
#3
-3
You are confusing class instance and instance variable. Class instance (also sometimes referred to as Object) is what @movie
is in your example.
您正在混淆类实例和实例变量。类实例(有时也称为Object)就是@movie的例子。
To put it in other words, you initialize/ instantiate a class when you do Class.new (Movie.new
above) there by creating a new instance of movie.
换句话说,当你通过创建一个新的电影实例来进行Class.new(上面的Movie.new)时,你初始化/实例化一个类。
isbn
on the other hand is an instance variable (also referred to as member variable sometimes) as it is defined within the class Movie
and accessible to each instance of that class. The reason it is called an instance variable is because each instance of class movie – say you have another instance called @movie2 = Movie.new("1234")
it will have its own copy of @isbn
with the value 1234
另一方面,isbn是一个实例变量(有时也称为成员变量),因为它在类Movie中定义,并且可供该类的每个实例访问。它被称为实例变量的原因是因为每个类电影的实例 - 比如说你有另一个叫做@ movie2 = Movie.new(“1234”)的实例,它将拥有自己的@isbn副本,其值为1234
an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance
实例变量是在类(即成员变量)中定义的变量,该类的每个实例化对象都有一个单独的副本或实例
#1
2
I've always thought instance variable as used only within the definition of a class and can be accessed inside the class.
我一直认为实例变量只在类的定义中使用,可以在类中访问。
That's wrong. Instance variables have nothing to do with classes at all. Instance variables belong to objects (aka instances), that's why they are called instance variables.
那是错的。实例变量与类完全无关。实例变量属于对象(也称为实例),这就是它们被称为实例变量的原因。
I'm confused that a instance variable appears outside of a class definition.
我很困惑,实例变量出现在类定义之外。
It is perfectly fine for an instance variable to appear outside of a class definition. After all, the other instance variables in your example are also not inside a class definition, they are in method definitions.
实例变量出现在类定义之外是完全正确的。毕竟,示例中的其他实例变量也不在类定义中,它们在方法定义中。
Why use @movie instead of local variable "movie"?
为什么使用@movie而不是局部变量“movie”?
There is no reason to do that in the code snippet you posted. But if it were part of a larger program, there certainly could be good reasons to do so.
在您发布的代码段中没有理由这样做。但如果它是一个更大的计划的一部分,那么肯定有充分的理由这样做。
#2
0
Instance variables have another scope than local variables. Outside a class you are still in the main Object class as you can see in the error produced in the following example. So in my example if I had to use the movie variable in a method like test I need an instance variable or (better) I would need to pass it as a parameter.
实例变量具有另一个范围而不是局部变量。在类之外,您仍然在主Object类中,您可以在以下示例中生成的错误中看到。所以在我的例子中,如果我必须在像测试这样的方法中使用movie变量,我需要一个实例变量或(更好)我需要将它作为参数传递。
In this case they are not much better than a global variable in poluting the namespace. Couldn't find a good article about Ruby and using variables and namespaces although I read them in the past. If I do, I'll add it to the answer.
在这种情况下,它们在规范名称空间时并不比全局变量好多少。虽然我过去读过它们但找不到关于Ruby和使用变量和命名空间的好文章。如果我这样做,我会把它添加到答案中。
class Movie
def initialize(isbn)
@isbn = isbn
end
def isbn
@isbn
end
end
@movie = Movie.new("0702")
@movie.isbn
movie = "you won't see me"
def test
p @movie # #<Movie:0x00000002d1e7f8 @isbn="0702">
p movie # error undefined local variable or method `movie' for main:Object
end
test
#3
-3
You are confusing class instance and instance variable. Class instance (also sometimes referred to as Object) is what @movie
is in your example.
您正在混淆类实例和实例变量。类实例(有时也称为Object)就是@movie的例子。
To put it in other words, you initialize/ instantiate a class when you do Class.new (Movie.new
above) there by creating a new instance of movie.
换句话说,当你通过创建一个新的电影实例来进行Class.new(上面的Movie.new)时,你初始化/实例化一个类。
isbn
on the other hand is an instance variable (also referred to as member variable sometimes) as it is defined within the class Movie
and accessible to each instance of that class. The reason it is called an instance variable is because each instance of class movie – say you have another instance called @movie2 = Movie.new("1234")
it will have its own copy of @isbn
with the value 1234
另一方面,isbn是一个实例变量(有时也称为成员变量),因为它在类Movie中定义,并且可供该类的每个实例访问。它被称为实例变量的原因是因为每个类电影的实例 - 比如说你有另一个叫做@ movie2 = Movie.new(“1234”)的实例,它将拥有自己的@isbn副本,其值为1234
an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance
实例变量是在类(即成员变量)中定义的变量,该类的每个实例化对象都有一个单独的副本或实例