0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

privateメソッドを呼び出し、オーバーライドする

Posted at

スーパークラスのprivateメソッドはサブクラスでも呼び出せる

irb(main):001* class Product
irb(main):002*   private
irb(main):003*
irb(main):004*   def name
irb(main):005*     'A great movie'
irb(main):006*   end
irb(main):007> end
=> :name
irb(main):008* class DVD <Product
irb(main):009*   def to_s
irb(main):010*     "name:#{name}"
irb(main):011*   end
irb(main):012> end
=> :to_s
irb(main):013> dvd = DVD.new
=> #<DVD:0x000000010bdc8168>
irb(main):014> dvd.to_s
=> "name:A great movie"

privateメソッドはオーバーライドできる

irb(main):001* class Product
irb(main):002*   def to_s
irb(main):003*     "name:#{name}"
irb(main):004*   end
irb(main):005*
irb(main):006*   private
irb(main):007*
irb(main):008*   def name
irb(main):009*     "A great movie"
irb(main):010*   end
irb(main):011> end
=> :name
irb(main):012* class DVD < Product
irb(main):013*   private
irb(main):014*
irb(main):015*   def name
irb(main):016*     "An awesome film"
irb(main):017*   end
irb(main):018> end
=> :name
irb(main):019> pruoduct = Product.new
=> #<Product:0x000000010f344c10>

irb(main):021> pruoduct.to_s
=> "name:A great movie"
irb(main):022> dvd = DVD.new
=> #<DVD:0x000000010f37f108>
irb(main):023> dvd.to_s
=> "name:An awesome film"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?