인터페이스 interface
클래스를 만들 때 사용하는 규약?
기본구조
interface [인터페이스 이름]
{
}
생성위치
인터페이스는 클래스와 동급의 카테고리이다. 그렇기 때문에 클래스를 생성하는 위치라면 어디든지 만들 수 있다.
예제를 보면서 이해하자
1) 인터페이스 생성
interface IBasic
{
int TestInstanceMethod( );
int TestProperty{ get; set ;}
}
메서드에 내부 구현을 입력할 수 없다.
속성에도 마찬가지로 내부 구현을 입력할 수 없다.
2) 인터페이스 상속
class TestClass : IBasic
{
}
static void Main(string[] args)
{
}
}
3) 인터페이스 구현
'Ctrl' + '.' 누르면 자동완성
class TestClass : IBasic
{
public int TestInstanceMethod( )
{
throw new NotImplementedException( );
}
public int TestProperty
{
get
{
throw new NotImplementedException( );
}
set
{
throw new NotImplementedException( );
}
}
}
클래스에서 이후에 구현해준다.
4) 인터페이스 다중 상속
class Program
{
class Parent{ }
class Child : Parent, IDisposable, IComparable
{
public void Dispose( )
{
throw new NotImplementedException( );
}
public int CompareTo(object obj)
{
throw new NotImplementedException( );
}
}
//한 개의 클래스와 두 개의 인터페이스를 상속받는다.
이렇게 상속되면 다음과 같이 세종류로 자료형 변환이 가능해진다.
Child child = new Child( );
Parent ChildAsParent = new Child( );
IDisposable childAsDisposable = new Child( );
IComparable childAsComparable = new Child( );
'프로그래밍언어 > C#' 카테고리의 다른 글
C#) 델리게이트 #2 (0) | 2019.01.24 |
---|---|
C#) 델리게이트(delegate) #1 (0) | 2019.01.23 |
c#) 스태틱 Static 변수,메서드,클래스 (4) | 2019.01.16 |
C#) 스택(Stack), 힙(Heap) 메모리구조 (0) | 2019.01.15 |
C#) continue , break 키워드 (0) | 2018.11.14 |