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