스토리지

[3.17] 인터페이스 연습 2 본문

Unity/수업내용(C#)

[3.17] 인터페이스 연습 2

ljw4104 2021. 3. 17. 13:15

using System;

namespace Study07
{
    public class App
    {
        public App()
        {
            Adventurer adventurer = new Adventurer(5000, 100, "Paladin");
            Console.WriteLine(adventurer);
            adventurer.Attack();
            adventurer.ChangeJob("Hero");
            Console.WriteLine();

            Resistance resistance = new Resistance(4500, 150, "Blaster");
            Console.WriteLine(resistance);
            resistance.Attack();
            Console.WriteLine();

            Anima anima = new Anima(3100, 200, "Hoyoung");
            Console.WriteLine(anima);
            anima.Attack();
        }
    }
}
using System;

namespace Study07
{
    public class User
    {
        protected int hp;
        protected int level;
        protected string jobName;

        public User()
        {
            Console.WriteLine("User 생성자.");
        }

        public virtual void Attack()
        {
            Console.WriteLine("공격하다.");
        }

        public override string ToString()
        {
            return String.Format("{0} {1} {2}", this.hp, this.level, this.jobName);
        }
    }
}
using System;

namespace Study07
{
    interface IChangeJob
    {
        bool ChangeJob(string s);
    }
}
using System;

namespace Study07
{
    public class Adventurer : User, IChangeJob
    {
        public Adventurer()
        {
            Console.WriteLine("Adventurer 생성자.");
        }

        public Adventurer(int hp, int level, string jobName)
        {
            this.hp = hp;
            this.level = level;
            this.jobName = jobName;
            Console.WriteLine("Adventurer 생성자.");
        }

        public override void Attack()
        {
            Console.WriteLine("모험가가 공격함.");
        }

        public bool ChangeJob(string s)
        {
            Console.WriteLine("{0}에서 {1}(으)로 자유전직합니다.", this.jobName, s);
            this.jobName = s;
            return this.jobName == s;
        }
    }
}
using System;

namespace Study07
{
    public class Resistance : User
    {
        public Resistance()
        {
            Console.WriteLine("Resistance 생성자.");
        }

        public Resistance(int hp, int level, string jobName)
        {
            this.hp = hp;
            this.level = level;
            this.jobName = jobName;
            Console.WriteLine("Resistance 생성자.");
        }

        public override void Attack()
        {
            Console.WriteLine("레지스탕스가 공격함.");
        }
    }
}
using System;

namespace Study07
{
    public class Anima : User
    {
        public Anima()
        {
            Console.WriteLine("Anima 생성자.");
        }

        public Anima(int hp, int level, string jobName)
        {
            this.hp = hp;
            this.level = level;
            this.jobName = jobName;
            Console.WriteLine("Anima 생성자.");
        }

        public override void Attack()
        {
            Console.WriteLine("아니마가 공격함.");
        }
    }
}

'Unity > 수업내용(C#)' 카테고리의 다른 글

[3.17] Queue 연습 1  (0) 2021.03.17
[3.17] Stack 연습 1  (0) 2021.03.17
[3.17] 인터페이스 연습 1  (0) 2021.03.17
[3.17] 배열 및 컬렉션 복습 2  (0) 2021.03.17
[3.17] 배열 및 컬렉션 복습 1  (0) 2021.03.17
Comments