Human 클래스, Human 을 상속받은 Adult, Baby 클래스 존재
일반적 타입 : Human
특수한 타입 : Adult, Baby
<aside> 🎀 부모 → 자식으로 캐스팅 형변환 ⇒ 불가능 자식 → 부모로 캐스팅 형변환 ⇒ 가능
</aside>
암시적 형변환 : 특수한 타입 → 일반적 타입
Adult Lina = new Adult(); Human lina = (Human)Lina; → 가능
명시적 형변환 : 일반적 타입 → 특수한 타입 ⇒ 불가능!!
Human Lina = new Human(); Adult lina = (Adult)Lina; → 에러! 불가능
Human Lina = new Adult();
이렇게 인스턴스화 하면 된다
이 Lina 객체는 Human 일까? Adult 일까?
⇒ Adult이다.
객체가 생성되면서 부모의 생성자인 “Human”을 출력하고 본인의 생성자인 “Adult”를 출력하는 모습
<aside> 🎪 자식객체는 인스턴스화 되면서 부모의 생성자도 호출한다!
</aside>
as : 에러 없이 형변환 시도
변수명 as 클래스명
→ 이 변수를 클래스명의 클래스로 형변환을 시도하겠다!
is : 형변환이 가능한지 체크
변수명 is 클래스명
→ 이 변수가 이 클래스로 형변환이 가능한지 True/False로 대답해줘
// Adult는 Human을 상속받은 클래스
Human human = new Human();
Human adult = new Adult();
Adult Jina = (Adult)adult; // adult는 이미 adult라서 형변환 안해도 될듯
Adult Lina = adult as Adult; // 이것도 adult는 이미 adult라서 형변환 안해도 될듯
Debug.Log(human is Human);