Way*_*rad 149
类变量(@@)在类及其所有后代之间共享.@类的后代不共享类实例变量().
类变量(@@)
让我们有一个带有类变量的类Foo @@i,以及用于读写的访问器@@i:
class Foo
@@i = 1
def self.i
@@i
end
def self.i=(value)
@@i = value
end
end
Run Code Online (Sandbox Code Playgroud)
派生类:
class Bar < Foo
end
Run Code Online (Sandbox Code Playgroud)
我们看到Foo和Bar具有相同的值@@i:
p Foo.i # => 1
p Bar.i # => 1
Run Code Online (Sandbox Code Playgroud)
改变@@i一个改变它们:
Bar.i = 2
p Foo.i # => 2
p Bar.i # => 2
Run Code Online (Sandbox Code Playgroud)
类实例变量(@)
让我们创建一个带有类实例变量的简单类@i和用于读写的访问器@i:
class Foo
@i = 1
def self.i
@i
end
def self.i=(value)
@i = value
end
end
Run Code Online (Sandbox Code Playgroud)
派生类:
class Bar < Foo
end
Run Code Online (Sandbox Code Playgroud)
我们看到虽然Bar继承了访问器@i,但它不会继承@i自己:
p Foo.i # => 1
p Bar.i # => nil
Run Code Online (Sandbox Code Playgroud)
我们可以设置Bar @i而不影响Foo @i:
Bar.i = 2
p Foo.i # => 1
p Bar.i # => 2
Run Code Online (Sandbox Code Playgroud)
hor*_*guy 70
首先,您必须了解类也是实例 - 类的实例Class.
一旦理解了这一点,就可以理解一个类可以将实例变量与它相关联,就像常规(读取:非类)对象一样.
Hello = Class.new
# setting an instance variable on the Hello class
Hello.instance_variable_set(:@var, "good morning!")
# getting an instance variable on the Hello class
Hello.instance_variable_get(:@var) #=> "good morning!"
Run Code Online (Sandbox Code Playgroud)
请注意,在一个实例变量Hello是完全无关的且不同于一个实例变量上的实例的Hello
hello = Hello.new
# setting an instance variable on an instance of Hello
hello.instance_variable_set(:@var, :"bad evening!")
# getting an instance variable on an instance of Hello
hello.instance_variable_get(:@var) #=> "bad evening!")
# see that it's distinct from @var on Hello
Hello.instance_variable_get(:@var) #=> "good morning!"
Run Code Online (Sandbox Code Playgroud)
一类变量,另一方面是一种以上两者的结合,因为它可以访问Hello本身及其实例,以及在子类Hello和它们的实例:
HelloChild = Class.new(Hello)
Hello.class_variable_set(:@@class_var, "strange day!")
hello = Hello.new
hello_child = HelloChild.new
Hello.class_variable_get(:@@class_var) #=> "strange day!"
HelloChild.class_variable_get(:@@class_var) #=> "strange day!"
hello.singleton_class.class_variable_get(:@@class_var) #=> "strange day!"
hello_child.singleton_class.class_variable_get(:@@class_Var) #=> "strange day!"
Run Code Online (Sandbox Code Playgroud)
很多人说要避免class variables因为上面的奇怪行为,而建议使用class instance variables代替.
| 归档时间: |
|
| 查看次数: |
30849 次 |
| 最近记录: |