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

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

Uniqueness of implemented interfaces




The interfaces implemented by a generic type declaration must remain unique for all possible constructed types. Without this rule, it would be impossible to determine the correct method to call for certain constructed types. For example, suppose a generic class declaration were permitted to be written as follows:

interface I<T>
{
void F();
}

class X<U,V>: I<U>, I<V> // Error: I<U> and I<V> conflict
{
void I<U>.F() {...}
void I<V>.F() {...}
}

Were this permitted, it would be impossible to determine which code to execute in the following case:

I<int> x = new X<int,int>();
x.F();

To determine if the interface list of a generic type declaration is valid, the following steps are performed:

· Let L be the list of interfaces directly specified in a generic class, struct, or interface declaration C.

· Add to L any base interfaces of the interfaces already in L.

· Remove any duplicates from L.

· If any possible constructed type created from C would, after type arguments are substituted into L, cause two interfaces in L to be identical, then the declaration of C is invalid. Constraint declarations are not considered when determining all possible constructed types.

In the class declaration X above, the interface list L consists of I<U> and I<V>. The declaration is invalid because any constructed type with U and V being the same type would cause these two interfaces to be identical types.

It is possible for interfaces specified at different inheritance levels to unify:

interface I<T>
{
void F();
}

class Base<U>: I<U>
{
void I<U>.F() {…}
}

class Derived<U,V>: Base<U>, I<V> // Ok
{
void I<V>.F() {…}
}

This code is valid even though Derived<U,V> implements both I<U> and I<V>. The code

I<int> x = new Derived<int,int>();
x.F();

invokes the method in Derived, since Derived<int,int> effectively re-implements I<int> (§13.4.6).







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


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


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