#!/usr/bin/ruby -w
# 定義類
class Box
# 構(gòu)造器方法
def initialize(w,h)
@width, @height = w, h
end
# 訪問(wèn)器方法
def getWidth
@width
end
def getHeight
@height
end
# 設(shè)置器方法
def setWidth=(value)
@width = value
end
def setHeight=(value)
@height = value
end
end
# 創(chuàng)建對(duì)象
box = Box.new(10, 20)
# 讓我們凍結(jié)該對(duì)象
box.freeze
if( box.frozen? )
puts "Box object is frozen object"
else
puts "Box object is normal object"
end
# 現(xiàn)在嘗試使用設(shè)置器方法
box.setWidth = 30
box.setHeight = 50
# 使用訪問(wèn)器方法
x = box.getWidth()
y = box.getHeight()
puts "Width of the box is : #{x}"
puts "Height of the box is : #{y}"