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

EXPRESSIONS

Method Invocation Expressions

15.12

class Test {

public static void main(String[] args) { new T3().test();

}

}

This program produces the output:

x=

3

super.x=

2

((T2)this).x=

2

((T1)this).x=

1

((I)this).x=

0

Within class T3, the expression super.x is treated as if it were:

((T2)this).x

Note that super.x is not specified in terms of a cast, due to difficulties around access to protected members of the superclass.

15.12 Method Invocation Expressions

A method invocation expression is used to invoke a class or instance method.

MethodInvocation:

MethodName ( ArgumentListopt )

Primary . NonWildTypeArgumentsopt Identifier ( ArgumentListopt ) super . NonWildTypeArgumentsopt Identifier ( ArgumentListopt )

ClassName . super . NonWildTypeArgumentsopt Identifier ( ArgumentListopt ) TypeName . NonWildTypeArguments Identifier ( ArgumentListopt )

The definition of ArgumentList from §15.9 is repeated here for convenience:

ArgumentList:

Expression

ArgumentList , Expression

Resolving a method name at compile time is more complicated than resolving a field name because of the possibility of method overloading. Invoking a method at run-time is also more complicated than accessing a field because of the possibility of instance method overriding.

451

15.12.1 Compile-Time Step 1: Determine Class or Interface to Search

EXPRESSIONS

Determining the method that will be invoked by a method invocation expression involves several steps. The following three sections describe the compile-time processing of a method invocation; the determination of the type of the method invocation expression is described in §15.12.3.

15.12.1 Compile-Time Step 1: Determine Class or Interface to Search

The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. There are several cases to consider, depending on the form that precedes the left parenthesis, as follows.

If the form is MethodName, then there are three subcases:

If it is a simple name, that is, just an Identifier, then the name of the method is the Identifier.

If the Identifier appears within the scope (§6.3) of a visible method declaration with that name, then:

If there is an enclosing type declaration of which that method is a member, let T be the innermost such type declaration. The class or interface to search is T.

Otherwise, the visible method declaration may be in scope due to one or more single-static-import (§7.5.3) or static-import-on-demand (§7.5.4) declarations. There is no class or interface to search, as the method to be invoked is determined later (§15.12.2).

If it is a qualified name of the form TypeName . Identifier, then the name of the method is the Identifier and the class to search is the one named by the

TypeName.

If TypeName is the name of an interface rather than a class, then a compile-time error occurs, because this form can invoke only static methods and interfaces have no static methods.

In all other cases, the qualified name has the form FieldName . Identifier.

The name of the method is the Identifier and the class or interface to search is the declared type T of the field named by the FieldName, if T is a class or interface type, or the upper bound of T if T is a type variable.

If the form is Primary . NonWildTypeArgumentsopt Identifier, then the name of the method is the Identifier.

452

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