class

# 定义类名的首字母应该大写
class Height
  # @@类变量属于类
  @@total = 0.to_mm
  def initialize(flr,roomht,strgt)
    # @实例变量可以跨任何特定的实例或对象中的方法使用
    @floor_id = flr
    @room_height = roomht
    @structure_height = strgt
  end
  # 类实例化后才可以调用方法
  def display_details
    puts "Floor: #@floor_id"
    puts "Floor height: #{(@room_height + @structure_height).to_mm}mm"
  end
  def total_height
    @@total += (@room_height + @structure_height)
    puts "Total height up to #{@floor_id}F: #{@@total.to_mm}mm"
  end
  # 加了Class.可以直接调用
  def Height.describe
    puts "建筑高度统计"
  end
end
f1=Height.new(1,2200.mm,500.mm)
f1.display_details
# Floor: 1
# Floor height: 2700.0mm
f1.total_height
# Total height up to 1F: 2700.0mm
f2=Height.new(2,2500.mm,600.mm)
f2.total_height
© AWhouse