Студопедия Главная Случайная страница Обратная связь

Разделы: Автомобили Астрономия Биология География Дом и сад Другие языки Другое Информатика История Культура Литература Логика Математика Медицина Металлургия Механика Образование Охрана труда Педагогика Политика Право Психология Религия Риторика Социология Спорт Строительство Технология Туризм Физика Философия Финансы Химия Черчение Экология Экономика Электроника

Interface implementations




Interfaces may be implemented by classes and structs. To indicate that a class or struct directly implements an interface, the interface identifier is included in the base class list of the class or struct. For example:

interface ICloneable
{
object Clone();
}

interface IComparable
{
int CompareTo(object other);
}

class ListEntry: ICloneable, IComparable
{
public object Clone() {...}

public int CompareTo(object other) {...}
}

A class or struct that directly implements an interface also directly implements all of the interface’s base interfaces implicitly. This is true even if the class or struct doesn’t explicitly list all base interfaces in the base class list. For example:

interface IControl
{
void Paint();
}

interface ITextBox: IControl
{
void SetText(string text);
}

class TextBox: ITextBox
{
public void Paint() {...}

public void SetText(string text) {...}
}

Here, class TextBox implements both IControl and ITextBox.

When a class C directly implements an interface, all classes derived from C also implement the interface implicitly. The base interfaces specified in a class declaration can be constructed interface types (§4.4). A base interface cannot be a type parameter on its own, though it can involve the type parameters that are in scope. The following code illustrates how a class can implement and extend constructed types:

class C<U,V> {}

interface I1<V> {}

class D: C<string,int>, I1<string> {}

class E<T>: C<int,T>, I1<T> {}

The base interfaces of a generic class declaration must satisfy the uniqueness rule described in §13.4.2.







Дата добавления: 2015-09-07; просмотров: 185. Нарушение авторских прав


Рекомендуемые страницы:


Studopedia.info - Студопедия - 2014-2020 год . (0.002 сек.) русская версия | украинская версия