다음을 통해 공유


제네릭 인터페이스(Visual C++)

제한이 클래스의 형식 매개 변수에는적용하다되어 동일 해당적용하다하려면 인터페이스에서 형식 매개 변수 (참조 하십시오 제네릭 클래스(C++/CLI)).

함수오버 로드를 제어 하는 규칙은 제네릭 클래스 또는 제네릭 인터페이스 내의 함수에 대 한 동일 합니다.

명시적인터페이스멤버 구현을인터페이스생성 된 형식으로 간단한인터페이스형식 (다음 예 참조)와 동일한 방법으로 작동 합니다.

인터페이스에 대 한 자세한 내용은 interface 클래스(C++ 구성 요소 확장).

[attributes] generic <class-key type-parameter-identifier[, ...]>
[type-parameter-constraints-clauses][accesibility-modifiers] interface class identifier [: base-list] {   interface-body} [declarators] ;

설명

  • 특성 (옵션)
    추가 선언 정보입니다.특성 및특성클래스에 대 한 자세한 내용은 특성을 참조 하십시오.

  • 클래스-키
    클래스 또는 유형 이름

  • type-parameter-identifier(s)
    쉼표로 구분 된 식별자 목록입니다.

  • type-parameter-constraints-clauses
    지정 된양식사용제네릭 형식 매개 변수에 대한 제약 조건(C++/CLI)

  • 접근성-한정자 (옵션)
    액세스 가능성 한정자 (예:공개 / 개인).

  • identifier
    인터페이스이름입니다.

  • 자료 목록 (옵션)
    쉼표로 구분 된 하나 이상의 명시적인 기본 인터페이스를 포함 하는 목록입니다.

  • 인터페이스-본문
    인터페이스멤버를 선언 합니다.

  • 선언 자 (옵션)
    이 형식을 기반으로 하는 변수를 선언 합니다.

예제

다음 예제에서는선언하다하는 방법 및 generic인터페이스를 인스턴스화합니다.예제에서는 제네릭인터페이스IList<ItemType> 선언 됩니다. 다음 두 제네릭 클래스에 의해 구현 된 List1<ItemType> 및 List2<ItemType>, 다른 구현을 사용 합니다.

// generic_interface.cpp
// compile with: /clr
using namespace System;

// An exception to be thrown by the List when
// attempting to access elements beyond the
// end of the list.
ref class ElementNotFoundException : Exception {};

// A generic List interface
generic <typename ItemType>
public interface class IList {
   ItemType MoveFirst();
   bool Add(ItemType item);
   bool AtEnd();
   ItemType Current();
   void MoveNext();
};

// A linked list implementation of IList
generic <typename ItemType>
public ref class List1 : public IList<ItemType> {
   ref class Node {
      ItemType m_item;

   public:
      ItemType get_Item() { return m_item; };
      void set_Item(ItemType value) { m_item = value; };
      
      Node^ next;

      Node(ItemType item) {
         m_item = item;
         next = nullptr;
      }
   };

   Node^ first;
   Node^ last;
   Node^ current;

   public:
   List1() {
      first = nullptr;
      last = first;
      current = first;
   }

   virtual ItemType MoveFirst() {
      current = first;
      if (first != nullptr)
        return first->get_Item();
      else
         return ItemType();
   }

   virtual bool Add(ItemType item) {
      if (last != nullptr) { 
         last->next = gcnew Node(item);
         last = last->next;
      }
      else {
         first = gcnew Node(item);
         last = first;
         current = first;
      }
      return true;
   }

   virtual bool AtEnd() {
      if (current == nullptr )
        return true;
      else 
        return false;
   }

   virtual ItemType Current() {
       if (current != nullptr)
         return current->get_Item();
       else
         throw gcnew ElementNotFoundException();
   }

   virtual void MoveNext() {
      if (current != nullptr)
       current = current->next;
      else
        throw gcnew ElementNotFoundException();
   }
};

// An array implementation of IList
generic <typename ItemType>
ref class List2 : public IList<ItemType> {
   array<ItemType>^ item_array;
   int count;
   int current;

   public:

   List2() {
      // not yet possible to declare an
      // array of a generic type parameter
      item_array = gcnew array<ItemType>(256);
      count = current = 0;
   }

   virtual ItemType MoveFirst() {
      current = 0;
      return item_array[0];
   }

   virtual bool Add(ItemType item) {
      if (count < 256)
         item_array[count++] = item;
      else
        return false;
      return true;
   }

   virtual bool AtEnd() {
      if (current >= count)
        return true;
      else
        return false;
   }
   
   virtual ItemType Current() {
      if (current < count)
        return item_array[current];
      else
        throw gcnew ElementNotFoundException();
   }

   virtual void MoveNext() {
      if (current < count) 
         ++current;
      else
         throw gcnew ElementNotFoundException();
   }
};

// Add elements to the list and display them.
generic <typename ItemType>
void AddStringsAndDisplay(IList<ItemType>^ list, ItemType item1, ItemType item2) {
   list->Add(item1);
   list->Add(item2);
   for (list->MoveFirst(); ! list->AtEnd(); list->MoveNext())
     Console::WriteLine(list->Current());
}

int main() {
   // Instantiate both types of list.

   List1<String^>^ list1 = gcnew List1<String^>();
   List2<String^>^ list2 = gcnew List2<String^>();

   // Use the linked list implementation of IList.
   AddStringsAndDisplay<String^>(list1, "Linked List", "List1");
      
   // Use the array implementation of the IList.
   AddStringsAndDisplay<String^>(list2, "Array List", "List2");
}
  

이 예제에서는 제네릭인터페이스를 선언 IMyGenIface, 및 두 개의 제네릭이 아닌 인터페이스를 IMySpecializedInt 및 ImySpecializedString는 전문 IMyGenIface.두 가지 특수 인터페이스 다음 두 가지 클래스에 의해 구현 된 MyIntClass 및 MyStringClass.이 예제에서는 제네릭 인터페이스를 전문적으로, 인스턴스화할 인터페이스, 제네릭 및 제네릭이 아닌 인터페이스를 명시적으로 구현 된 멤버를 호출 하는 방법을 보여 줍니다.

// generic_interface2.cpp
// compile with: /clr
// Specializing and implementing generic interfaces.
using namespace System;

generic <class ItemType>
public interface class IMyGenIface {
   void Initialize(ItemType f);
};

public interface class IMySpecializedInt: public IMyGenIface<int> {
   void Display();
};

public interface class IMySpecializedString: public IMyGenIface<String^> {
   void Display();
};

public ref class MyIntClass: public IMySpecializedInt {
   int myField;

public: 
   virtual void Initialize(int f) {
      myField = f;
   }

   virtual void Display() {
      Console::WriteLine("The integer field contains: {0}", myField);
   }    
};

public ref struct MyStringClass: IMySpecializedString {    
   String^ myField;

public:
   virtual void Initialize(String^ f) {
      myField = f;
    }

   virtual void Display() {
      Console::WriteLine("The String field contains: {0}", myField);
   }
};

int main() {
   // Instantiate the generic interface.
   IMyGenIface<int>^ myIntObj = gcnew MyIntClass();

   // Instantiate the specialized interface "IMySpecializedInt."
   IMySpecializedInt^ mySpIntObj = (IMySpecializedInt^) myIntObj;

   // Instantiate the generic interface.
   IMyGenIface<String^>^ myStringObj = gcnew MyStringClass();

   // Instantiate the specialized interface "IMySpecializedString."
   IMySpecializedString^ mySpStringObj = 
            (IMySpecializedString^) myStringObj;

   // Call the explicitly implemented interface members.
   myIntObj->Initialize(1234);
   mySpIntObj->Display();

   myStringObj->Initialize("My string");
   mySpStringObj->Display();
}
  

참고 항목

기타 리소스

제네릭(C++ 구성 요소 확장)