Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_language_specification_7.pdf
Скачиваний:
34
Добавлен:
21.03.2016
Размер:
3.11 Mб
Скачать

4.4 Type Variables TYPES, VALUES, AND VARIABLES

• They are both class or both interface types, are defined by the same class loader, and have the same binary name (§13.1), in which case they are sometimes said to be the same run-time class or the same run-time interface.

• They are both array types, and their component types are the same run-time type (§10).

4.4 Type Variables

A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.

A type variable is declared as a type parameter of a generic class declaration (§8.1.2), generic interface declaration (§9.1.2), generic method declaration (§8.4.4), or generic constructor declaration (§8.8.4).

TypeParameter:

TypeVariable TypeBoundopt

TypeBound:

extends TypeVariable

extends ClassOrInterfaceType AdditionalBoundListopt

AdditionalBoundList:

AdditionalBound AdditionalBoundList

AdditionalBound

AdditionalBound:

& InterfaceType

The scope of a type variable declared as a type parameter is specified in §6.3.

Every type variable declared as a type parameter has a bound. If no bound is declared for a type variable, Object is assumed. If a bound is declared, it consists of either:

a single type variable T, or

a class or interface type T possibly followed by interface types I1 & ... & In.

It is a compile-time error if any of the types I1 ... In is a class type or type variable.

58

TYPES, VALUES, AND VARIABLES

Type Variables

4.4

The erasures (§4.6) of all constituent types of a bound must be pairwise different, or a compile-time error occurs.

A type variable must not at the same time be a subtype of two interface types which are different parameterizations of the same generic interface, or a compile-time error occurs.

The order of types in a bound is only significant in that the erasure of a type variable is determined by the first type in its bound, and that a class type or type variable may only appear in the first position.

The members of a type variable X with bound T & I1 & ... & In are the members of the intersection type (§4.9) T & I1 & ... & In appearing at the point where the type variable is declared.

Example 4.4-1. Members of a Type Variable

package TypeVarMembers;

class C {

 

 

 

public

void

mCPublic()

{}

protected void

mCProtected() {}

 

void

mCDefault()

{}

private

void

mCPrivate()

{}

}

 

 

 

interface I { void mI();

}

class CT extends C implements I { public void mI() {}

}

class Test {

<T extends C & I> void test(T t) {

t.mI();

// OK

t.mCPublic();

// OK

t.mCProtected();

// OK

t.mCDefault();

//

OK

t.mCPrivate();

//

Compile-time error

}

}

The type variable T has the same members as the intersection type C & I, which in turn has the same members as the empty class CT, defined in the same scope with equivalent supertypes. The members of an interface are always public, and therefore always inherited (unless overridden). Hence mI is a member of CT and of T. Among the members of C, all but mCPrivate are inherited by CT, and are therefore members of both CT and T.

59

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]