今日も継承について学んでみた!
今日の学び
インスタンス変数を用いる際はselfをつける
- 例
*class Food
*arttr_accessor :name
*arttr_accessor :calorie
*def calorie_info
*return "#{self.name}は#{self.price}円です"
*end
*end
継承されてないインスタンスメソッドを追加する
- 子クラスに独自のインスタンス変数を追加できたように継承されてないインスタンスメソッドを追加する
- 例
class Food < Menu
arttr_accessor :calorie
def calorie_info
return "#{self.name}は#{self.price}円です"
end
end
- 例