class Bike
attr_reader :gears
def initialize(g = 5)
@gears = g
end
end
class AnotherBike < Bike
attr_reader :seats
def initialize(g, s = 2)
super(g)
@seats = s
end
end
是否可以创建一个 AnotherBike 实例“AnotherBike.new”,当未给出参数时,该实例将从 super 获取“gears”的默认值?
所以例如
my_bike = AnotherBike.new
...
my_bike.gears #=> 5
my_bike.seats #=> 2
my_bike = AnotherBike.new(10)
...
my_bike.gears #=> 10
my_bike.seats #=> 2
my_bike = AnotherBike.new(1,1)
...
my_bike.gears #=> 1
my_bike.seats #=> 1
我正在使用 Ruby 1.9.3。