K-digital traning/C#프로그래밍

대리자 연습 - Hero HitDamage

내꺼블로그 2023. 7. 27. 14:35

(1)Hero.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class Hero
    {
        public int hp;
        public int maxHp;

        //생성자
        public Hero()
        {
            this.hp = 10;
            this.maxHp = this.hp;
        }


        //남은체력
        public void HitDamage(int damage, Action<int, int> callback)
        {
            this.hp -= damage;
            callback(this.hp, this.maxHp);
        }

        //죽었니 살았니
        public void HitDamage(int damage, Action<bool> callback) {
            this.hp -= damage;
            callback(this.hp <= 0);
        }
    }
}

 

 

 

 

(2)App.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class App
    {

        //생성자
        public App()
        {
            Hero hero = new Hero();
            hero.HitDamage(3, (hp, maxhp) => {
                Console.WriteLine("{0}/{1}", hp, maxhp);
            });

            hero.HitDamage(3, (isDie) => { Console.WriteLine("isDie: {0}", isDie); });
        }
    }
}

 

 

 

 

결과