源代码:
点击运行
#!/usr/bin/ruby -w # 定义类 class Box BOX_COMPANY = "TATA Inc" BOXWEIGHT = 10 # 构造器方法 def initialize(w,h) @width, @height = w, h end # 实例方法 def getArea @width * @height end end # 创建对象 box = Box.new(10, 20) # 调用实例方法 a = box.getArea() puts "Area of the box is : #{a}" puts Box::BOX_COMPANY puts "Box weight is: #{Box::BOXWEIGHT}"
运行结果