블로그 이미지
내게 능력 주시는 자 안에서 내가 모든것을 할수 있느니라 - 빌립보서 4 : 13 - happydong

카테고리

Happydong (1363)
프로그래밍 (156)
MUSIC (16)
인물 (3)
Utility (10)
세미나 소식&내용 (22)
IT뉴스 (18)
운동 (830)
CAFE (10)
Life (282)
Total
Today
Yesterday



인터페이스(interface)

 

 - class가 제공하는 서비스를 명시하는데 사용되는 행위(method/property/indexer/event)의 집합

 - 세부적인 구현은 하지 않고, 단지 실제 class에서 해야 할 행동을 정의

   . 각 멤버의 signature만 정의

   . method,property,event,indexer를 하나로 묶어주는 역할

 - 실제 세부적인 구현은 interface를 상속받는 class에서 구현

 - class 가 다중상속을 지원하지 못하는 것을 보완하는 기능


인터페이스의 특징

 

  - method, property,indexer,event 만 포함될 수 있다.

    . const,field,constructor,destructor,static member 등은 포함될 수 없다.

  - 기본적으로 public member

  - 인터페이스에 대한 객체(인스턴스)를 생성할 수 없다.

  - interface 에서 정의된 모든 member는 파생 class에서 반드시 모두 구현되어야 한다.

  - 인터페이스의 이름은 I로 시작하는 것이 일반적이다.

  - 하나의 class를 다른 class가 상속받을 수 있듯이 인터페이스도 다른 인터페이스가 상속을    

    수 있다.


인터페이스의 정의

 

 - interface를 구현하는 class의 해당 method 에는 접근자(public,private,protected등)와 abstract,

  virtual , override, new등의 수식자를 저정할 수 없다.

 - System.Collections namespace 의 IEnumerator

////////////////////////////////////////////////////////////////////////////////////////

 interface IEnumerator{

          //member property

       object Current { get; } // 모든 member는 default로 public


         // member method

       bool MoveNext(); // interface의 모든 member는 정의만 있고 구현은 없다.

       void Reset();

}

////////////////////////////////////////////////////////////////////////////////////////

이렇게 member의 정의만 있고 구현은 없다.

이렇게 interface에서 상속을 받는 class 에서는 구현을 해줘야 한다.

///////////////////////////////// 예 제 /////////////////////////////////////////////////

class CustomEnumerator : IEnumerator

{

           public bool MoveNext()

           {

                 //...구체내용

                 return false;

           }

           public void Reset()

           {

                   pos = 0;

           }

           public object Current{

                  get{ return carArray[pos]; }

           }

}

////////////////////////////////////////////////////////////////////////////////////////


인터페이스의 사용


 - 인터페이스는 직접 객체를 생성할 수 없고, 인터페이스를 상속받은 class의 객체를 통해 사용된다.

///////////////////////////////////예  제//////////////////////////////////////////////////

         CustomEnumerator ce = new CustomEnumerator();

         bool bMove = ce.MoveNext();

         ce.Reset();

////////////////////////////////////////////////////////////////////////////////////////



참고 서적 : 소설같은 C#

Posted by happydong
, |