백지부터 시작하는 이세계 코딩 생활
Abstract (추상화), Interface (인터페이스) 본문
Abstact
추상매서드는 상속관계에 있는 상위클래스의 매서드를 하위클래스에서 재정의 (override) 하도록 강제할 때 사용한다.
따라서 반드시 재정의가 필요한 매서드를 사용하고자 할 때 abstract 로 정의한다.
추상클래스 (Abstract Class) 그리고 추상매서드 (Abstract Method)란 서로 분리시켜 쓸 수 있는 개념이 아니다.
추상매서드는 매서드의 바디 (구현부)가 없어야 한다. 추상매서드의 구현은 하위클래스에서 이뤄지기 때문이다.
cf. 추상매서드를 사용함으로써 operate error 도 줄일 수 있으며 유지보수시 가독성 또한 높일 수 있다.
규칙 또는 조건 ( rule ) :
* 상위클래스의 매서드는 반드시 하위클래스에서 매서드의 재정의 (override) 강제가 발생한다.
* abstract 키워드를 적는다(구분짓기 위해). 추상매서드가 정의되었다면 클래스도 추상클래스로 정의해줘야 한다.
* 추상클래스에서 일반 매서드와 일반 속성의 정의도 가능하다. (모두 추상매서드일 필요는 없다)
* 추상클래스는 인스턴스 (Instance) 생성이 불가능하다.
* 추상매서드는 하위클래스 매서드에서 재정의하여 구현해야 한다. (override 기법)
using UnityEngine;
public class Abstract : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Left left = new Left();
Right rigth = new Right();
left.Hand();
rigth.Hand();
left.Foot();
rigth.Foot();
}
abstract class Doll
{
public abstract void Hand();
public abstract void Foot();
public void Head()
{
print("here is Doll's Head");
}
public void Body()
{
print("this is Doll's Body");
}
}
private class Left : Doll
{
public override void Hand()
{
print("here is Doll's left hand");
}
public override void Foot()
{
print("here is Doll's left foot");
}
}
private class Right : Doll
{
public override void Hand()
{
print("this is Doll's right hand");
}
public override void Foot()
{
print("this is Doll's right foot");
}
}
}
Interface
인터페이스란 (Interface), 상속관계를 가질 때, 다중상속이 불가능하다는 애로사항을 해결하기 위해 도입된 개념이다.
i.e ) 인터페이스는 다중상속을 가능하게 한다.
인터페이스로 정의할 때는 알파벳 I (아이) 를 붙여주는 것이 관례다.
cf. 인터페이스는 어떤 매서드를 제공하는지 미리 알려주는 명세서 (또는 약속) 같은 역할도 한다.
인터페이스를 통해 해당 클래스를 어떻게 사용할 것인지 알 수 있게 된다.
인터페이스는 abstract 키워드와 public 접근제한자를 생략하고 사용한다.
인터페이스에는 오직 추상클래스로만 정의되기 때문이며,
인터페이스의 모든 맴버는 접근제한자가 public으로 정의되어 있기 때문이다.
규칙 또는 조건 ( rule ) :
* interface 키워드를 통해 정의된다.
* 인터페이스의 바디 (구현부)는 추상 맴버만 정의할 수 있다.
* 인터페이스에 구현된 멤버를 하위클래스에서 구현하지 않으면 에러가 발생한다.
* 인터페이스는 인스턴스를 생성할 수 없다.
* 인터페이스의 맴버는 public으로 구현해야 한다.
* 상속은 "클래스 이름 : 인터페이스의" 이름 형태로 사용할 수 있다.
#region INTERFACE 정의
public class Interface
{
interface IFireball
{
void FireballCasting();
}
interface IBlizzard
{
void BlizzardCasting();
}
interface IEnergyShild
{
void EnergyShildCasting();
}
}
#endregion
using UnityEngine;
#region NAMESPACE 정의
namespace Spell
{
public abstract class Spell { }
public interface IFireball { void FireballCasting(); }
public interface IBlizzard { void BlizzardCasting(); }
public interface IEnergyShild { void EnergyShildCasting(); }
}
#endregion
public class Casting : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Cast cast = new Cast();
cast.FireballCasting();
cast.BlizzardCasting();
cast.EnergyShildCasting();
}
public class Cast : Spell.IFireball, Spell.IBlizzard, Spell.IEnergyShild
{
//override
public void FireballCasting()
{
print("FireballCasting...!");
}
public void BlizzardCasting()
{
print("BlizzardCasting...!");
}
public void EnergyShildCasting()
{
print("EnergyShildCasting...!");
}
}
}
'백지부터 시작하는 이세계 유니티 생활 since 2020' 카테고리의 다른 글
Sorting Layer (0) | 2020.12.24 |
---|---|
Image property in Inspector (0) | 2020.12.14 |
Object-Oriented Programming; OOP (객체지향프로그래밍) (2) | 2020.12.10 |
Member, Field, Method, Function (0) | 2020.12.10 |
Property, Attribute (속성) (0) | 2020.12.09 |