App下載

ruby 在線工具

#!/usr/bin/ruby -w

# 定義類
class Box
   BOX_COMPANY = "TATA Inc"
   BOXWEIGHT = 10
   # 構(gòu)造器方法
   def initialize(w,h)
      @width, @height = w, h
   end
   # 實(shí)例方法
   def getArea
      @width * @height
   end
end

# 創(chuàng)建對(duì)象
box = Box.new(10, 20)

# 調(diào)用實(shí)例方法
a = box.getArea()
puts "Area of the box is : #{a}"
puts Box::BOX_COMPANY
puts "Box weight is: #{Box::BOXWEIGHT}"
運(yùn)行結(jié)果