演習課題「RPGのプレイヤーを継承で書こう - その1」
配列にプレイヤークラスのオブジェクトを代入し、順番にAttackメソッドを呼び出しスライムを攻撃してください。
期待する出力値
=== パーティーでドラゴンと戦う ===
勇者はスライムを攻撃した
戦士はスライムを攻撃した
演習課題「RPGのプレイヤーを継承で書こう - その1」
右のコードエリアではPlayerクラスの勇者、戦士が作成され、Attackメソッドを呼び出しています。
PlayerクラスのAttackメソッドを実装して、スライムを攻撃してください。
期待する出力値
=== パーティーでスライムと戦う ===
勇者はスライムを攻撃した
戦士はスライムを攻撃した
#04:RPGのプレイヤーを継承で書こう - その1
ここでは、クラスを継承する具体例として、RPGのPlayerクラスとWizardクラスを書きます。はじめに、基底クラスとなる、Playerクラスから書いてきます。
// RPGのプレイヤーを継承で書こう - その1
using System;
class Lesson08
{
    public static void Main()
    {
        Console.WriteLine("=== パーティーでスライムと戦う ===");
        var hero = new Player("勇者");
        // hero.Attack("スライム");
        var warrior = new Player("戦士");
        Player[] party = { hero, warrior };
        
        foreach(var player in party)
        {
            player.Attack("スライム");
        }
    }
}
class Player
{
    public string Name { get; private set; }
    public Player(string name)
    {
        Name = name;
    }
    public void Attack(string enemy)
    {
        Console.WriteLine(Name + "は、" + enemy + "を攻撃した!");
    }
}
C# クラスの継承のサンプル | ITSakura
https://itsakura.com/csharp-inheritance
継承 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C
https://ufcpp.net/study/csharp/oo_inherit.html
C# での継承 | Microsoft Docs
https://docs.microsoft.com/ja-jp/dotnet/csharp/tutorials/inheritance