泛型介面 (Visual C++)
套用至類別中的參數的限制與套用至介面中的參數的項目 (請參閱 泛型類別 (C++/CLI))。
控制函式多載的規則與泛型類別或泛型介面中的函式相同。
明確介面成員實作與建構的介面型別,以使用與簡單介面型別的方式 (請參閱以下範例)。
如需介面的詳細資訊,請參閱interface class (C++ 元件擴充功能)。
[attributes] generic <class-key type-parameter-identifier[, ...]>
[type-parameter-constraints-clauses][accesibility-modifiers] interface class identifier [: base-list] { interface-body} [declarators] ;
備註
屬性 (選擇性的)。
補充宣告資訊。 如需關於屬性及屬性類別的詳細資訊,請參閱屬性。class-key
class 和 typenametype-parameter-identifier(s)
逗號分隔識別項的清單。type-parameter-constraints-clauses
指定在 泛型類型參數的條件約束 (C++/CLI) 中拿到表格。存取範圍修飾詞 (選擇性)
Accessibility modifiers (如 public, private)。identifier
介面名稱。基底清單 (選擇性)
包含一個或多個明確基底以逗號分隔介面的清單。介面主體
介面成員的宣告。宣告子 (選擇性)
根據型別變數的宣告。
範例
下列範例示範如何宣告及執行個體化泛型介面。 在此範例中, 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();
}