ruby 在線工具
#!/usr/bin/ruby -w
class Box
# 初始化類變量
@@count = 0
def initialize(w,h)
# 給實例變量賦值
@width, @height = w, h
@@count += 1
end
def self.printCount()
puts "Box count is : #@@count"
end
end
# 創(chuàng)建兩個對象
box1 = Box.new(10, 20)
box2 = Box.new(30, 100)
# 調(diào)用類方法來輸出盒子計數(shù)
Box.printCount()