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

[유니티] 객체 배열/업캐스팅

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();

        //1. 기본
        HumanBefore human1 = new HumanBefore();
        human1.name = "인간1";
        HumanBefore human2 = new HumanBefore();
        human2.name = "인간2";

        MonstorBefore monstor1 = new MonstorBefore();
        monstor1.name = "몬스터1";
        MonstorBefore monstor2 = new MonstorBefore();
        monstor2.name = "몬스터2";
        MonstorBefore monstor3 = new MonstorBefore();
        monstor3.name = "몬스터3";

        //2. 개선(배열이용)
        HumanBefore[] humans = new HumanBefore[2];
        humans[0] = human1;
        humans[1] = human2;

        for (int i=0; i<humans.Length; i++)
        {
            humans[i].Move();
        }

        MonstorBefore[] monstors = new MonstorBefore[3];
        monstors[0] = monstor1;
        monstors[1] = monstor2;
        monstors[2] = monstor3;

        for (int i = 0; i < monstors.Length; i++)
        {
            monstors[i].Move();
        }

        //3. 순서가 다를때(배열 쓸 수 없을때) 한계 극복


    }

    // 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 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 CSharpExtendEx : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Human human = new Human("인간", 100, 23, 10, 1, "안녕하세요. 저는 유저입니다.");
        //human.Move();
        
        Monstor monstor = new Monstor("몬스터", 100, 23, 3);
        //monstor.Move();
        
        Pet pet = new Pet("피카츄", 100, 23, 2);
        //pet.Move();

        //업캐스팅 사용
        Character upHuman = (Character)human;

        //upHuman.Move();

        Character upMonstor = (Character)monstor;
        //upMonstor.Move();

        Character upPet = (Character)pet;
        //upPet.Move();

        //캐릭터 참조 배열 만들기
        Character[] characters = new Character[3];
        characters[0] = upHuman;
        characters[1] = upMonstor;
        characters[2] = upPet;

        for (int i = 0; i<characters.Length; i++)
        {
            characters[i].Move();
        }
    }

    // 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 Character(string name, int hp, int damage)
    {
        this.name = name;
        this.hp = hp;
        this.damage = damage;
    }

    public virtual void Move() // 가상메소드 추가 ---> override 추가
    {
        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 override void Move()
    {
        Debug.Log(name + "님이 공중에 떠서 이동을 합니다.");
    }
    public void Buff()
    {
        Debug.Log(name + " 님이 " + buffType + "타입의 버프를 캐릭터에게 제공합니다.");
    }
}
public class CSharpExtendEx : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Human human = new Human("인간", 100, 23, 10, 1, "안녕하세요. 저는 유저입니다.");
        //human.Move();
        
        Monstor monstor = new Monstor("몬스터", 100, 23, 3);
        //monstor.Move();
        
        Pet pet = new Pet("피카츄", 100, 23, 2);
        //pet.Move();

        //업캐스팅 사용
        Character upHuman = (Character)human;

        //upHuman.Move();

        Character upMonstor = (Character)monstor;
        //upMonstor.Move();

        Character upPet = (Character)pet;
        //upPet.Move();
        //pet 업케스팅했다면 참조로 접근할수 없기 때문에 가상메소드로 만든다.
        //upPet[&] ---> [v:Move()] >>> [o:Move()]

        //캐릭터 참조 배열 만들기
        Character[] characters = new Character[3];
        characters[0] = upHuman;
        characters[1] = upMonstor;
        characters[2] = upPet;

        for (int i = 0; i<characters.Length; i++)
        {
            characters[i].Move();
        }
    }

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

    }
}

댓글