https://hongjinhyeon.tistory.com/93
interface Params 를 쓰는 경우 예시를 찾아보면
public interface IApple{
int I {get; set;} // 맛의 성분 1
int J {get; set;} // 맛의 성분 2
}
// 초록색 사과
public class GrannySmith : IApple{
public GrannySmith()
{
this.I = 10;
this.J = 6;
}
int I {get; set;}
int J {get; set;}
}
// 빨간색 사과
public class PinkLady : IApple{
public PinkLady()
{
this.I = 42;
this.J = 1;
}
int I {get; set;}
int J {get; set;}
}
public class FruitUtils{
// 사과맛
public int CalculateAppleness(IApple apple)
{
return apple.J * apple.I;
}
}
interface 를 정의한 후 사용법
var apple = new GrannySmith();
var result = new FruitUtils().CalculateAppleness(apple);
var apple2 = new PinkLady();
var result2 = new FruitUtils().CalculateAppleness(apple2);
Console.WriteLine(result); //Produces 60
Console.WriteLine(result2); //Produces 42
사용이유 와 차이점
1. 확장성, 코딩실수에 편의를 가져다 준다.
public class FruitUtils{
// GrannySmith Appleness
public int CalculateGrannySmithAppleness(GrannySmith gsAppleness)
{
return gsAppleness.J * gsAppleness.I;
}
// PinkLady Appleness
public int CalculatePinkLadyAppleness(PinkLady plAppleness)
{
return plAppleness.J * plAppleness.I;
}
}
각각의 CalculateGrannySmithAppleness, CalculatePinkLadyAppleness 함수의 매개변수가 GrannySmith 거나 PinkLady 면 각각의 사과맛은 계산이 되지만 만약 IApple 을 상속받는 종류가 더 많아지면 확장하기 힘들어진다. 다분히 객체지향적이다. 그리고 interface 에서 정의된 몸통없는 메소드, 프로퍼티, 인덱서 를 강제하므로 스펠링에러가 생기는걸 방지한다.
아는데 참 안써진다...
'Unity' 카테고리의 다른 글
[Unity] 유니티 설계 (0) | 2022.12.07 |
---|---|
[Unity Error] Unity ARMv7 ARM64 빌드시 크래시 에러 (0) | 2022.12.05 |
[Unity] 10가지 팁 by TaroDev (0) | 2022.11.15 |
유니티 그래픽스 최적화 - GI (Global Illumination) 정리 (0) | 2022.11.11 |
iOS용 AppStore 배포 등록절차 (0) | 2022.11.02 |