유니티로 배우는 c#

상속

테오구 2021. 10. 10. 13:00
728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

abstract public class Human : MonoBehaviour
{
    public string humanName;
    public int humanAge;

    protected virtual void info(){ //virtual은 자식 클래스에서 재정의 할 수 있게 해주는 녀석
        print("나는 인간입니다.");

    }

    // 추상함수
    // 자식 클래스에서 기능을 완성시켜야 하는 함수
    // 이걸 쓰게 되면 클래스 자체도 미완성이 되기 때문에 class에도 abstract를 써야한다.
    abstract protected void Name();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class student : Human //Human 클래스에 있는 humanName과 humanAge를 사용하기위해 상속
{
    string schoolName;

    // Start is called before the first frame update
    void Start()
    {
        schoolName = "애플콕 초등학교";
        humanName = "애플콕";
        humanAge = 27;

        info();
    }

// virtual 가상함수
// override 재정의
    protected override void info(){
        base.info(); //부모클래스의 info
        print("나는 학생 입니다.");
        print(humanAge);
    }

    protected override void Name(){
        print(humanName);
    }
}

728x90

'유니티로 배우는 c#' 카테고리의 다른 글

인덱서  (0) 2021.10.10
프로퍼티  (0) 2021.10.10
델리게이트  (0) 2021.10.10
구조체  (0) 2021.10.10
네임스페이스  (0) 2021.10.10