看下面範例就知道啦. 其中 cattr_accessor 產生的變數就是 @@class_count attr_accessor = @instance_count
class Counter
cattr_accessor :class_count
attr_accessor :instance_count
end
counter1 = Counter.new
counter1.instance_count = 1
counter1.class_count = 1
counter2 = Counter.new
p counter2.instance_count
#> nil
p counter2.class_count
#> 1
沒有留言:
張貼留言