本地和实例变量之间的区别

时间:2022-09-15 23:09:36

I understand that local variables are limited to the scope they were declared in and instance variables exist as long as the class exists, but what happens if you declare a local variable in the class scope without prefixing it with @? Doesn't that implicitly it is an instance variable, even though you didn't use an @ to declare it as one?

我知道局部变量仅限于它们声明的范围,并且只要该类存在,实例变量就存在,但是如果在类范围内声明局部变量而不使用@作为前缀,会发生什么?这不是隐含地它是一个实例变量,即使你没有使用@来声明它作为一个变量吗?

2 个解决方案

#1


2  

instance variables exist as long as the class exists

只要类存在,就存在实例变量

They exist as long as the object exist. Instance variables are per-object, not per-class.

只要对象存在,它们就存在。实例变量是每个对象,而不是每个类。

what happens if you declare a local variable in the class scope without prefixing it with @?

如果你在类范围内声明一个局部变量而不用@作为前缀,会发生什么?

Then the variable is in scope within the class definition, but not within any defs inside that class definition as those introduce a new scope.

然后变量在类定义中的范围内,但不在该类定义中的任何defs内,因为它们引入了新的范围。

Doesn't that implicitly make it an instance variable, even though you didn't use an @ to declare it as one?

这不是隐含地使它成为实例变量,即使你没有使用@来声明它作为一个变量吗?

No.

没有。

If you use define_method instead of def to create methods, the local variable will be accessible within the methods, but since the variable only exists once (not once per object), they'd act more like class variables than instance variables in that case. I also can't think of a good reason why you'd use them that way.

如果使用define_method而不是def来创建方法,则可以在方法中访问局部变量,但由于变量只存在一次(每个对象不存在一次),因此在这种情况下它们更像是类变量而不是实例变量。我也想不出你为什么这么用它们的好理由。

#2


1  

Using @makes it an instance variable for an object that you create. When you are doing things with that object you can set local variables but they disappear after use. Instance variables will stay around as long as there is an object.

使用@makes它为您创建的对象的实例变量。当您使用该对象执行操作时,可以设置局部变量,但在使用后它们会消失。只要有对象,实例变量就会保持不变。

#1


2  

instance variables exist as long as the class exists

只要类存在,就存在实例变量

They exist as long as the object exist. Instance variables are per-object, not per-class.

只要对象存在,它们就存在。实例变量是每个对象,而不是每个类。

what happens if you declare a local variable in the class scope without prefixing it with @?

如果你在类范围内声明一个局部变量而不用@作为前缀,会发生什么?

Then the variable is in scope within the class definition, but not within any defs inside that class definition as those introduce a new scope.

然后变量在类定义中的范围内,但不在该类定义中的任何defs内,因为它们引入了新的范围。

Doesn't that implicitly make it an instance variable, even though you didn't use an @ to declare it as one?

这不是隐含地使它成为实例变量,即使你没有使用@来声明它作为一个变量吗?

No.

没有。

If you use define_method instead of def to create methods, the local variable will be accessible within the methods, but since the variable only exists once (not once per object), they'd act more like class variables than instance variables in that case. I also can't think of a good reason why you'd use them that way.

如果使用define_method而不是def来创建方法,则可以在方法中访问局部变量,但由于变量只存在一次(每个对象不存在一次),因此在这种情况下它们更像是类变量而不是实例变量。我也想不出你为什么这么用它们的好理由。

#2


1  

Using @makes it an instance variable for an object that you create. When you are doing things with that object you can set local variables but they disappear after use. Instance variables will stay around as long as there is an object.

使用@makes它为您创建的对象的实例变量。当您使用该对象执行操作时,可以设置局部变量,但在使用后它们会消失。只要有对象,实例变量就会保持不变。