irb(main):001* class Product
irb(main):002* attr_reader :name, :price
irb(main):003*
irb(main):004* def initialize(name, price)
irb(main):005* @name = name
irb(main):006* @price = price
irb(main):007* end
irb(main):008> end
=> :initialize
irb(main):009> product = Product.new("A great movie", 1000)
=> #<Product:0x00000001124ad0e0 @name="A great movie", @price=1000>
irb(main):010> product.name
=> "A great movie"
irb(main):011> product.price
=> 1000
irb(main):012* class DVD < Product
irb(main):013* attr_reader :running_time
irb(main):014*
irb(main):015* def initialize(name, price, running_time)
irb(main):016* @name = name
irb(main):017* @price = price
irb(main):018* @running_time = running_time
irb(main):019* end
irb(main):020> end
=> :initialize
irb(main):021> dvd = DVD.new("A great movie", 1000, 120)
=> #<DVD:0x000000011294c830 @name="A great movie", @price=1000, @running_time=120>
irb(main):022> dvd.name
=> "A great movie"
irb(main):023> dvd.price
=> 1000
irb(main):024> dvd.running_time
=> 120
superを使うとスーパークラスの同じ名前のメソッドを使える
irb(main):001* class Product
irb(main):002* attr_reader :name, :price
irb(main):003*
irb(main):004* def initialize(name, price)
irb(main):005* @name = name
irb(main):006* @price = price
irb(main):007* end
irb(main):008> end
=> :initialize
irb(main):009* class DVD < Product
irb(main):010* attr_reader :running_time
irb(main):011*
irb(main):012* def initialize(name, price, running_time)
irb(main):013* super(name, price)
irb(main):014*
irb(main):015* @running_time = running_time
irb(main):016* end
irb(main):017> end
=> :initialize
irb(main):018> dvd = DVD.new("A great movie", 1000, 120)
=> #<DVD:0x000000010bc538c8 @name="A great movie", @price=1000, @running_time=120>
irb(main):019> dvd.name
=> "A great movie"
irb(main):020> dvd.price
=> 1000
irb(main):021> dvd.running_time
=> 120
superだけで使える
スーパークラスとサブクラスで引数の数が同じだった場合は、引数なしのsuperを呼ぶだけで自分に渡された引数をすべてスーパークラスに引き渡すことができる
irb(main):001* class Product
irb(main):002* attr_reader :name, :price
irb(main):003*
irb(main):004* def initialize(name, price)
irb(main):005* @name = name
irb(main):006* @price = price
irb(main):007* end
irb(main):008> end
=> :initialize
irb(main):009* class DVD < Product
irb(main):010* attr_reader :running_time
irb(main):011*
irb(main):012* def initialize(name, price)
irb(main):013* super
irb(main):014* end
irb(main):015> end
=> :initialize
# 引数の数が同じであればできる。
irb(main):016> dvd = DVD.new("A great movie",1000)
=> #<DVD:0x0000000112f02220 @name="A great movie", @price=1000>
# 引数の数が違うと失敗する
irb(main):017> dvd = DVD.new("A great movie",1000, 120)
(irb):12:in `initialize': wrong number of arguments (given 3, expected 2) (ArgumentError)
...
super()だと違う
super()と書いた場合は、「引数0個でスーパークラスの同名メソッドを呼び出す」の意味になるので注意してください。
irb(main):001* class Product
irb(main):002* attr_reader :name, :price
irb(main):003*
irb(main):004* def initialize(name, price)
irb(main):005* @name = name
irb(main):006* @price = price
irb(main):007* end
irb(main):008> end
=> :initialize
irb(main):009* class DVD < Product
irb(main):010* attr_reader :name, :price
irb(main):011*
irb(main):012* def initialize(name, price)
irb(main):013* super()
irb(main):014* end
irb(main):015> end
=> :initialize
irb(main):016> dvd = DVD.new("A Great movie", 1000)
(irb):4:in `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)
...
サブクラスの内容がスーパークラスとも同じなら書かなくていい
irb(main):001* class Product
irb(main):002* attr_reader :name, :price
irb(main):003*
irb(main):004* def initialize(name, price)
irb(main):005* @name = name
irb(main):006* @price = price
irb(main):007* end
irb(main):008> end
=> :initialize
irb(main):009* class DVD < Product
irb(main):010> end
=> nil
irb(main):011> dvd = DVD.new("A great movie", 1000)
=> #<DVD:0x00000001031b2db8 @name="A great movie", @price=1000>
感想
super、super()が違うことに気をつけよう。
superだけで引き継ぐことができる