Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 4 Advanced Class Design

Points to Remember

The following points about local classes are apt to come up in the OCPJP 7 exam:

You can create a non-static local class inside a body of code. Interfaces cannot have local classes, and you cannot create local interfaces.

Local classes are accessible only from the body of the code in which the class is defined. The local classes are completely inaccessible outside the body of the code in which the class is defined.

You can extend a class or implement interfaces while defining a local class.

A local class can access all the variables available in the body of the code in which it is defined. You can pass only final variables to a local inner class.

Anonymous Inner Classes

As the name implies, an anonymous inner class does not have a name. The declaration of the class automatically derives from the instance-creation expression. They are also referred to simply as anonymous classes.

An anonymous class is useful in almost all situations where you can use local inner classes. A local inner class has a name, whereas an anonymous inner class does not—and that’s the main difference! An additional difference is that an anonymous inner class cannot have any explicit constructors. A constructor is named after the name of the class, and since an anonymous class has no name, it follows that you cannot define a constructor!

(Before we proceed, just a note: there are no such things as “anonymous interfaces.”) Here is an example just to address the syntax of a local class:

class SomeClass {

void someFunction() { new Object() { };

}

}

This code looks cryptic, doesn’t it? What is going on here? In the statement new Object() { };, you are declaring a derived class of Object directly using the new keyword. It doesn’t define any code and returns an instance of that derived object. The created object is not used anywhere, so it is ignored. The new expression invokes the default constructor here; you could choose to invoke a multiple argument constructor of the base class by passing arguments in the new expression. When defining an anonymous class, it implicitly extends the base class (which is Object base class here).

Don’t worry if you didn’t understand this example. You’ll look now at a more practical example, and the usage of anonymous classes will become clearer.

Previously you saw the DescriptiveColor class (Listing 4-7) inside the getDescriptiveColor method in the StatusReporter class. You can simplify the code by converting the local class into an anonymous class, as shown in Listing 4-8.

Listing 4-8.  StatusReporter.java

class StatusReporter {

static Shape.Color getDesciptiveColor(final Shape.Color color) {

//note the use of anonymous inner classes here -- specifically, there is no name

//for the class and we construct and use the class "on the fly" in the return

//statement!

return new Shape.Color() {

102

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