0
0

More than 1 year has passed since last update.

【Ruby】継承とは | 初学者向け

Last updated at Posted at 2022-10-08

はじめに

継承とはどういうことなのか。
僕はRubyを学び始めた頃にあまり理解をしていなかったので、
これからRubyを学ぼうとしている方のために簡単にここに記しておく。

Rubyでクラスを継承する方法

そもそも継承とは

そもそもクラスを継承するとは、Google辞書で検索するとうけつぐことと表示される。
まさにその通り、ほかのクラスの内容をうけつぐことを継承という、らしい。

クラスの継承 | 具体例

class Takeshi
    def super_method
        puts "スーパー!!!!!!!!!!!!!"
    end
end

# Super クラスの機能(メソッド)を Sub クラスで継承(取り込む)する
class Gorira < Takeshi
end

# Super クラスのメソッドが利用できる
p Gorira.new.super_method
# => スーパー!!!!!!!!!!!!!

多分こんな感じになります。

これが継承です。以上

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