본문 바로가기

C# 개인 공부 기록용/어소트락 게임아카데미 - C# 무료강의[2019] 총48강

[C# 강의 15화] !중요! this 레퍼런스

출처 : https://www.youtube.com/watch?v=9HtZePD4GuY&list=PL4SIC1d_ab-Y-bBKojxhtFWwNpawMM1h5&index=15

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

class Player
{
    // 멤버변수
    // 객체가 생기면 만들어지게 되는
    private int HP = 100;

    // public void Damage(int _Dmg)
    // {
    //    // C#은 대체 어떻게?
    //    // 이 HP가 플레이어 2의
    //    // HP라는걸 알 수 있을까?
    //    // -> 플레이어가 2가 호출했으니 플레이어 2의 HP다 (90%의 정답)
    //    // HP -= _Dmg;
    //    // this.HP -= _Dmg;
    // }

    // 멤버함수의 호출이란
    // 어차피 넣을거 내가 대신 넣어줄게
    public void Heal(/*Player this,*/int _Heal)
    {
        // 멤버함수에서 
        // 멤버변수를 쓴다면
        // 눈에 보이지는 않지만
        // 앞에 this.이 생략된 것이다.
        // this.HP라는 것을 잊으면은 안되는데
        
        this.HP += _Heal;
    }

    public static void Damage(Player _this, int _Dmg)
    {
        // static 멤버함수는 객체를 만들지 않고도 사용할 수 있으므로
        // 자신이라는 개념이 없는 함수이다.
        // this.HP;
        _this.HP -= _Dmg;
    }

    // 정적 멤버변수만을 정적 멤버함수에서 사용할 수 있다.
    private static int StTest = 100;
    public static void PVP(Player _Left, Player _Right)
    {
        StTest = 50;
        // 객체에 영향을 받지 않는 것이 정적 멤버함수 정적 멤버변수
        // HP = 1000; -> 누구의 HP인지 몰라서 사용할 수 없다.
    }

}

namespace This
{
    class Program
    {
        static void Main(string[] args)
        {
            Player NewPlayer1 = new Player();
            Player NewPlayer2 = new Player();
            Player NewPlayer3 = new Player();

            NewPlayer3.Heal(/*NewPlayer3, */ 100);

            // NewPlayer1.Damage(100);
            Player.Damage(NewPlayer1, 100);

            Player.PVP(NewPlayer1, NewPlayer2);
        }
    }
}