본문 바로가기
Unity/C# 문법

[유니티] 상속 문법(부모/자식 class)

by Skull Crusher 2021. 4. 27.
728x90

1. 상속 문법 사용(전)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//속성 메소드 중복 => 수정해야할때 문제가 될 수 있음
//=> 상속으로 해결!
public class HumanBefore
{
    public string name; //중복
    public int hp; //중복
    public int damage; //중복
    public int level;
    public int gravity;
    public string talkMessage;

    public void Talk()
    {
        Debug.Log(name + "대화내용 : " + talkMessage);
    }

    //중복
    public void Move()
    {
        Debug.Log(name + " 님이 이동을 합니다.");
    }

    //중복
    public void Attack()
    {
        Debug.Log(name + " 님이 " + damage + "의 공격을 수행합니다.");
    }

    //중복
    public void Hit()
    {
        Debug.Log(name + " 님이 공격을 당해 피해를 입었습니다.");
    }

    public void Jump()
    {
        Debug.Log(name + " 님이 점프를 수행합니다.");
    }

    //중복
    public void Die()
    {
        Debug.Log(name + " 님이 사망하였습니다.");
    }
}

public class MonstorBefore
{
    public string name; //중복
    public int hp; //중복
    public int damage; //중복
    public int skillType;

    //중복
    public void Move()
    {
        Debug.Log(name + " 님이 이동을 합니다.");
    }

    //중복
    public void Attack()
    {
        Debug.Log(name + " 님이 " + damage + "의 공격을 수행합니다.");
    }

    //중복
    public void Hit()
    {
        Debug.Log(name + " 님이 공격을 당해 피해를 입었습니다.");
    }

    //중복
    public void Die()
    {
        Debug.Log(name + " 님이 사망하였습니다.");
    }

    public void Skill()
    {
        Debug.Log(name + " 님이 " + skillType + "번째 스킬을 발동합니다.");
    }
}

public class CSharpExtendBefore : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        HumanBefore human = new HumanBefore();
        human.name = "인간";
        human.hp = 100;
        human.damage = 23;
        human.level = 10;
        human.gravity = 1;
        human.talkMessage= "안녕하세요. 저는 유저입니다.";

        human.Move();
        human.Attack();
        human.Hit();
        human.Jump();
        human.Talk();
        human.Die();

        MonstorBefore monstor = new MonstorBefore();
        monstor.name = "몬스터";
        monstor.hp = 100;
        monstor.damage = 23;
        monstor.skillType = 3;

        monstor.Move();
        monstor.Attack();
        monstor.Hit();
        monstor.Skill();
        monstor.Die();

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

2. 상속 문법 사용(후)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//속성 메소드 중복 => 수정해야할때 문제가 될 수 있음
//=> 상속으로 해결!

// 부모클래스(중복된 클래스들의 공통된 의미로 이름 정하기)
public class Character
{
    public string name; //중복
    public int hp; //중복
    public int damage; //중복

    public void Move()
    {
        Debug.Log(name + " 님이 이동을 합니다.");
    }

    //중복
    public void Attack()
    {
        Debug.Log(name + " 님이 " + damage + "의 공격을 수행합니다.");
    }

    //중복
    public void Hit()
    {
        Debug.Log(name + " 님이 공격을 당해 피해를 입었습니다.");
    }

    //중복
    public void Die()
    {
        Debug.Log(name + " 님이 사망하였습니다.");
    }
}

// Character 클래스를 상속한 Human 클래스 선언
public class Human : Character
{
    public int level;
    public int gravity;
    public string talkMessage;

    public void Talk()
    {
        Debug.Log(name + "대화내용 : " + talkMessage);
    }

    public void Jump()
    {
        Debug.Log(name + " 님이 점프를 수행합니다.");
    }

}

public class Monstor : Character
{
    public int skillType;

    public void Skill()
    {
        Debug.Log(name + " 님이 " + skillType + "번째 스킬을 발동합니다.");
    }
}

public class Pet : Character
{
    public int buffType;

    public void Buff()
    {
        Debug.Log(name + " 님이 " + buffType + "타입의 버프를 캐릭터에게 제공합니다.");
    }
}
public class CSharpExtendAfter : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Human human = new Human();
        human.name = "인간";
        human.hp = 100;
        human.damage = 23;
        human.level = 10;
        human.gravity = 1;
        human.talkMessage= "안녕하세요. 저는 유저입니다.";

        human.Move();
        human.Attack();
        human.Hit();
        human.Talk();
        human.Jump();
        human.Die();

        Monstor monstor = new Monstor();
        monstor.name = "몬스터";
        monstor.hp = 100;
        monstor.damage = 23;
        monstor.skillType = 3;

        monstor.Move();
        monstor.Attack();
        monstor.Hit();
        monstor.Skill();
        monstor.Die();

        Pet pet = new Pet();
        pet.name = "피카츄";
        pet.hp = 100;
        pet.damage = 23;
        pet.buffType = 2;

        pet.Move();
        pet.Attack();
        pet.Hit();
        pet.Buff();
        pet.Die();


    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

출력되는 결과값은 같지만

중복되는 비슷한 속성들을 부모 클래스에 묶어놓으면

코드가 간결해지고, 수정에 용이하다.

 

3. 메소드 오버라이드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//속성 메소드 중복 => 수정해야할때 문제가 될 수 있음
//=> 상속으로 해결!

// 부모클래스(중복된 클래스들의 공통된 의미로 이름 정하기)
public class Character
{
    public string name; //중복
    public int hp; //중복
    public int damage; //중복

    public void Move()
    {
        Debug.Log(name + " 님이 이동을 합니다.");
    }

    //중복
    public void Attack()
    {
        Debug.Log(name + " 님이 " + damage + "의 공격을 수행합니다.");
    }

    //중복
    public void Hit()
    {
        Debug.Log(name + " 님이 공격을 당해 피해를 입었습니다.");
    }

    //중복
    public void Die()
    {
        Debug.Log(name + " 님이 사망하였습니다.");
    }
}

// Character 클래스를 상속한 Human 클래스 선언
public class Human : Character
{
    public int level;
    public int gravity;
    public string talkMessage;

    public void Talk()
    {
        Debug.Log(name + "대화내용 : " + talkMessage);
    }

    public void Jump()
    {
        Debug.Log(name + " 님이 점프를 수행합니다.");
    }

}

public class Monstor : Character
{
    public int skillType;

    public void Skill()
    {
        Debug.Log(name + " 님이 " + skillType + "번째 스킬을 발동합니다.");
    }
}

public class Pet : Character
{
    public int buffType;


    //자식클래스에서만 재정의(메소드 오버라이드)
    public void Move()
    {
        Debug.Log(name + "님이 공중에 떠서 이동을 합니다.");
    }
    public void Buff()
    {
        Debug.Log(name + " 님이 " + buffType + "타입의 버프를 캐릭터에게 제공합니다.");
    }
}
public class CSharpExtendAfter : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Human human = new Human();
        human.name = "인간";
        human.hp = 100;
        human.damage = 23;
        human.level = 10;
        human.gravity = 1;
        human.talkMessage= "안녕하세요. 저는 유저입니다.";

        human.Move();
        human.Attack();
        human.Hit();
        human.Talk();
        human.Jump();
        human.Die();

        Monstor monstor = new Monstor();
        monstor.name = "몬스터";
        monstor.hp = 100;
        monstor.damage = 23;
        monstor.skillType = 3;

        monstor.Move();
        monstor.Attack();
        monstor.Hit();
        monstor.Skill();
        monstor.Die();

        Pet pet = new Pet();
        pet.name = "피카츄";
        pet.hp = 100;
        pet.damage = 23;
        pet.buffType = 2;

        pet.Move();
        pet.Attack();
        pet.Hit();
        pet.Buff();
        pet.Die();


    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

4. 생성 변경하기

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//속성 메소드 중복 => 수정해야할때 문제가 될 수 있음
//=> 상속으로 해결!

// 부모클래스(중복된 클래스들의 공통된 의미로 이름 정하기)
public class Character
{
    public string name; //중복
    public int hp; //중복
    public int damage; //중복

    public void Move()
    {
        Debug.Log(name + " 님이 이동을 합니다.");
    }

    //중복
    public void Attack()
    {
        Debug.Log(name + " 님이 " + damage + "의 공격을 수행합니다.");
    }

    //중복
    public void Hit()
    {
        Debug.Log(name + " 님이 공격을 당해 피해를 입었습니다.");
    }

    //중복
    public void Die()
    {
        Debug.Log(name + " 님이 사망하였습니다.");
    }
}

// Character 클래스를 상속한 Human 클래스 선언
public class Human : Character
{
    public int level;
    public int gravity;
    public string talkMessage;

    public Human(string name, int hp, int damage, int level, int gravity, string talkMessage)
    {
        this.name = name;
        this.hp = hp;
        this.damage = damage;
        this.level = level;
        this.gravity = gravity;
        this.talkMessage = talkMessage;
    }

    public void Talk()
    {
        Debug.Log(name + "대화내용 : " + talkMessage);
    }

    public void Jump()
    {
        Debug.Log(name + " 님이 점프를 수행합니다.");
    }

}

public class Monstor : Character
{
    public int skillType;

    public Monstor(string name, int hp, int damage, int skillType)
    {
        this.name = name;
        this.hp = hp;
        this.damage = damage;
        this.skillType = skillType;

    }

    public void Skill()
    {
        Debug.Log(name + " 님이 " + skillType + "번째 스킬을 발동합니다.");
    }
}

public class Pet : Character
{
    public int buffType;

    public Pet(string name, int hp, int damage, int buffType)
    {
        this.name = name;
        this.hp = hp;
        this.damage = damage;
        this.buffType = buffType;

    }

    //자식클래스에서만 재정의(메소드 오버라이드)
    public void Move()
    {
        Debug.Log(name + "님이 공중에 떠서 이동을 합니다.");
    }
    public void Buff()
    {
        Debug.Log(name + " 님이 " + buffType + "타입의 버프를 캐릭터에게 제공합니다.");
    }
}
public class CSharpExtendAfter : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Human human = new Human("인간", 100, 23, 10, 1, "안녕하세요. 저는 유저입니다.");

        human.Move();
        human.Attack();
        human.Hit();
        human.Talk();
        human.Jump();
        human.Die();

        Monstor monstor = new Monstor("몬스터", 100, 23, 3);
        
        monstor.Move();
        monstor.Attack();
        monstor.Hit();
        monstor.Skill();
        monstor.Die();

        Pet pet = new Pet("피카츄", 100, 23, 2);

        pet.Move();
        pet.Attack();
        pet.Hit();
        pet.Buff();
        pet.Die();


    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

5. base 사용

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//속성 메소드 중복 => 수정해야할때 문제가 될 수 있음
//=> 상속으로 해결!

// 부모클래스(중복된 클래스들의 공통된 의미로 이름 정하기)
public class Character
{
    public string name; //중복
    public int hp; //중복
    public int damage; //중복

    public Character(string name, int hp, int damage)
    {
        this.name = name;
        this.hp = hp;
        this.damage = damage;
    }

    public void Move()
    {
        Debug.Log(name + " 님이 이동을 합니다.");
    }

    //중복
    public void Attack()
    {
        Debug.Log(name + " 님이 " + damage + "의 공격을 수행합니다.");
    }

    //중복
    public void Hit()
    {
        Debug.Log(name + " 님이 공격을 당해 피해를 입었습니다.");
    }

    //중복
    public void Die()
    {
        Debug.Log(name + " 님이 사망하였습니다.");
    }
}

// Character 클래스를 상속한 Human 클래스 선언
public class Human : Character
{
    public int level;
    public int gravity;
    public string talkMessage;

    //
    public Human(string name, int hp, int damage, int level, int gravity, string talkMessage) : base(name, hp, damage)
    {
        this.level = level;
        this.gravity = gravity;
        this.talkMessage = talkMessage;
    }

    public void Talk()
    {
        Debug.Log(name + "대화내용 : " + talkMessage);
    }

    public void Jump()
    {
        Debug.Log(name + " 님이 점프를 수행합니다.");
    }

}

public class Monstor : Character
{
    public int skillType;

    public Monstor(string name, int hp, int damage, int skillType) : base(name, hp, damage)
    {
        this.skillType = skillType;

    }

    public void Skill()
    {
        Debug.Log(name + " 님이 " + skillType + "번째 스킬을 발동합니다.");
    }
}

public class Pet : Character
{
    public int buffType;

    public Pet(string name, int hp, int damage, int buffType) : base(name, hp, damage)
    {
        this.buffType = buffType;

    }

    //자식클래스에서만 재정의(메소드 오버라이드)
    public void Move()
    {
        Debug.Log(name + "님이 공중에 떠서 이동을 합니다.");
    }
    public void Buff()
    {
        Debug.Log(name + " 님이 " + buffType + "타입의 버프를 캐릭터에게 제공합니다.");
    }
}
public class CSharpExtendAfter : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {        
        Human human = new Human("인간", 100, 23, 10, 1, "안녕하세요. 저는 유저입니다.");

        human.Move();
        human.Attack();
        human.Hit();
        human.Talk();
        human.Jump();
        human.Die();

        Monstor monstor = new Monstor("몬스터", 100, 23, 3);
        
        monstor.Move();
        monstor.Attack();
        monstor.Hit();
        monstor.Skill();
        monstor.Die();

        Pet pet = new Pet("피카츄", 100, 23, 2);

        pet.Move();
        pet.Attack();
        pet.Hit();
        pet.Buff();
        pet.Die();
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

댓글