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」 時沒有給出參數,將採取從超級「齒輪」默認值?紅寶石超值參數時在子類中缺少
所以e.g
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。
我想'super'會把所有ARGS,如果你不把它傳遞明確的,所以你應該叫'超()'這樣 – fl00r 2013-02-19 12:58:32