LoginSignup
1
1

More than 1 year has passed since last update.

C#学習 -継承-

Last updated at Posted at 2019-07-06

C#学習中のため、学習内容をメモしていく。
間違いやアドバイスがあればコメントからよろしくお願いします。

継承

親クラスのメンバーを子クラスが引き継ぐことができる。
継承するには : をつけてクラス宣言する。

class Cat : Animal{
 // Animalを基底クラス、親クラス、スーパークラスと呼ぶ
 // Catを派生クラス、子クラス、サブクラスと呼ぶ
}

コード例

// 親クラス
class Animal
{
    public string Kind;
    public string Cry;

    public string Show()
    {
        return $"{this.Kind}";
    }
}

// 子クラス
class Cat : Animal
{
    public string Name;

    // 子クラス独自のメソッドを定義可能
    public string GetCry()
    {
        // 親クラスの変数が利用可能
        return $"{this.Kind}{this.Cry}と鳴く";
    }
}

class Program
{
    static void Main(string[] args)
    {
        var cat = new Cat
        {
            Kind = "猫",
            Cry = "にゃー",
            Name = "ルミ"
        };
        // 親クラスで定義されたShowメソッドを
        // 子クラスのメンバーであるかのように呼び出せる
        Console.WriteLine(cat.Show());
        Console.WriteLine(cat.GetCry());
    }
}

メソッドの隠蔽

親クラスに存在するメンバーを子クラスに追加すると、子クラスのメンバーが優先され、親クラスのメンバーが見えなくなる。
隠蔽をするにはnewキーワードをつけて宣言する。

コード例

// 親クラスのShowメソッドを隠蔽
public new string Show()
{
    return $"{this.Kind}{this.Name}";
}

メソッドのオーバーライド

隠蔽と同じように親クラスのメンバーを再定義することができる。
親クラス側のメソッドにvirtual修飾子をつけて定義する。(仮想メソッド、仮想メンバーと呼ぶ)
子クラス側のメソッドにoverride修飾子をつけて定義する。
virtual無しのメソッドをオーバーライドすることはできない。

コード例

// 親クラス
class Animal
{
    public string Name { get;}

    public string Kind { get;}

    public Animal(string name, string kind)
    {
        Name = name;
        Kind = kind;
    }

    public virtual string Show()
        => $"{this.Kind}{this.Name}";
}

// 子クラス
class Cat : Animal
{
    public int Age { get; }

    public Cat(string name, string kind, int age) : base(name, kind)
    {
        Age = age;
    }

    public override string Show()
    {
        if (Age < 1)
        {
            return $"{this.Kind}の赤ちゃんの{this.Name}";
        }
        else
        {
            return base.Show();
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
            var cat1 = new Cat("くろ", "マンチカン", 5);
            var cat2 = new Cat("ミケ", "三毛猫", 0);

            Console.WriteLine(cat1.Show());
            Console.WriteLine(cat2.Show());
    }
}

実行結果

マンチカンのくろ
三毛猫の赤ちゃんのミケ

継承時のコンストラクターの動作

継承関係のあるクラスでは上位クラスから順にコンストラクターが呼び出される。

コード例

// 親クラス
class Animal
{
    public Animal()
    {
        Console.WriteLine("動物");
    }
}

// 子クラス
class Cat : Animal
{
    public Cat()
    {
        Console.WriteLine("猫");
    }
}

class Program
{
    static void Main(string[] args)
    {
        var cat = new Cat();
    }
}

実行結果

動物
猫

引数ありのコンストラクターを呼び出す場合は、baseキーワードを付加する。

コード例

// 親クラス
class Animal
{
    public Animal(string str)
    {
        Console.WriteLine($"動物が{str}です");
    }
}

// 子クラス
class Cat : Animal
{
    public Cat(string str) : base(str)
    {
        Console.WriteLine($"猫が{str}です");
    }
}

class Program
{
    static void Main(string[] args)
    {
        var cat = new Cat("好き");
    }
}

実行結果

動物が好きです
猫が好きです

継承の禁止

継承をさせたくない時はsealed修飾子を付与することで、継承そのものを禁止することができる。

コード例

// 親クラス
sealed class Animal
{
}

// 子クラス
class Cat : Animal  => エラーとなる
{
}

参考した本

独習C#

おわり。

1
1
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
1
1