728x90
SMALL

// アクセッサをプロパティに置き換えよう

 

using System;

 

public class Practice

{

    public static void Main()

    {

        var adventurer = new Adventurer("", 120);

        var wizard = new Adventurer("ウィザ", 549);

        var crusader = new Adventurer("クルセイダ", 50);

        var priest = new Adventurer("プリスト", 480);

 

        Adventurer[] party = {adventurer, wizard, crusader, priest};

 

        foreach (Adventurer player in party)

        {

            player.Attack();

            Console.WriteLine("MP" + player.GetMP());

        }

 

        Console.WriteLine("者の" + Adventurer.Count + "");

    }

}

 

class Adventurer

{

    private string job;

    private int mp;

 

    private static int count;

 

    public Adventurer(string job, int mp)

    {

        this.job = job;

        this.mp = mp;

        Count += 1;

    }

 

    public void Attack()

    {

        Console.WriteLine(job + "は魔王を攻した");

        mp -= 5;

    }

 

    public int GetMP()

    {

        return mp;

    }

 

 

 

    // Countプロパティに置き換える

public static int Count { get; private set; }

}

728x90

설정

트랙백

댓글