2015年11月5日 星期四

Ruby [ class variable, class instance variable, instance variable]

所以
@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

【下列文章您可能也有興趣】

沒有留言: