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

15.11.2 Accessing Superclass Members using super

EXPRESSIONS

15.11.2 Accessing Superclass Members using super

The form super.Identifier refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.

The form T.super.Identifier refers to the field named Identifier of the lexically enclosing instance corresponding to T, but with that instance viewed as an instance of the superclass of T.

The forms using the keyword super are valid only in an instance method, instance initializer, or constructor, or in the initializer of an instance variable of a class. If they appear anywhere else, a compile-time error occurs.

These are exactly the same situations in which the keyword this may be used (§15.8.3).

It is a compile-time error if the forms using the keyword super appear in the declaration of class Object, since Object has no superclass.

If a field access expression super.name appears within class C, and the immediate superclass of C is class S, then super.name is treated exactly as if it had been the expression this.name in the body of class S. Thus it can access the field name that is visible in class S, even if that field is hidden by a declaration of a field name in class C.

If a field access expression T.super.name appears within class C, and the immediate superclass of the class denoted by T is a class whose fully qualified name is S, then T.super.name is treated exactly as if it had been the expression this.name in the body of class S. Thus it can access the field name that is visible in class S, even if that field is hidden by a declaration of a field name in class T.

It is a compile-time error if the current class is not an inner class of class T or T itself.

Example 15.11.2-1. The super Expression

interface I

{ int x = 0; }

 

 

class T1 implements I { int x = 1; }

 

 

class T2 extends T1

{ int x = 2; }

 

 

class T3 extends T2 {

 

 

 

int x = 3;

 

 

 

void test() {

 

 

 

System.out.println("x=\t\t"

+

x);

System.out.println("super.x=\t\t"

+

super.x);

System.out.println("((T2)this).x=\t" +

((T2)this).x);

System.out.println("((T1)this).x=\t" +

((T1)this).x);

System.out.println("((I)this).x=\t"

+

((I)this).x);

}

 

 

 

}

 

 

 

450

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