@a 是 class variable
@b 是 class instance variable
@c 是 instance variable
class Hi
@@a = 1 # class variable
@b = 2 # class instance variable
def initialize
@c = 3 # instance variable
end
def test # instance method, works on objects of class A
puts @@a # => 1
puts @b # => nil, there is no instance variable @b
puts @c # => 3 # we defined this instance variable in the initialize
end
def self.test2 # instance method, works on objects of class A
puts @@a # => 1
puts @b # => 2
puts @c # => nil
end
end
沒有留言:
張貼留言