Ruby编程基础与Rails框架入门
1. Ruby控制结构与类的使用
在Ruby编程中,控制结构对于程序的逻辑执行至关重要。为了演示这些控制结构,我们先定义一些汽车类:
class Car
WHEELS = 4 # class constant
@@number_of_cars = 0 # class variable
def initialize
@@number_of_cars = @@number_of_cars + 1
end
def self.count
@@number_of_cars
end
def mileage=(x) # instance variable writer
@mileage = x
end
def mileage # instance variable reader
@mileage
end
end
class StretchLimo < Car
WHEELS = 6 # class constant
@@televisions = 1 # class variable
def turn_on_television
# Invoke code for switching on on-board TV here
end
end
class PontiacFirebird < Car
end
class VolksWagen < Car
超级会员免费看
订阅专栏 解锁全文
68

被折叠的 条评论
为什么被折叠?



