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

Chapter 4 Advanced Class Design

Shape.Color white = new Shape.Color(255, 255, 255); System.out.println("White color has values:" + white);

}

}

It prints

White color has: red = 255 green = 255 blue = 255

What do you observe in this code? The Shape class is declared abstract. You can see the Color class defined as a public static class defined within the Shape class. The TestColor class uses the syntax Shape.Color to refer to this class. Other than this minor difference, the Color class looks no different from defining the Color class outside the Shape class. Yes, that is a good observation. So, let’s repeat it to make it clear: a static nested class is as good as a class defined as an outer class with one difference—it is physically defined inside another class!

Points to Remember

Here are some notable aspects of static nested classes (and interfaces) that will help you on the OCPJP 7 exam:

The accessibility (public, protected, etc.) of the static nested class is defined by the outer class.

The name of the static nested class is expressed with OuterClassName.NestedClassName syntax.

When you define an inner nested class (or interface) inside an interface, the nested class is declared implicitly public and static. This point is easy to remember: any field in an

interface is implicitly declared public and static, and static nested classes have this same behavior.

Static nested classes can be declared abstract or final.

Static nested classes can extend another class or it can be used as a base class.

Static nested classes can have static members. (As you’ll see shortly, this statement does not apply to other kinds of nested classes.)

Static nested classes can access the members of the outer class (only static members, obviously).

The outer class can also access the members (even private members) of the nested class through an object of nested class. If you don’t declare an instance of the nested class, the outer class cannot access nested class elements directly.

Inner Classes

You can define a class (or an interface) as a non-static member inside another class. How about declaring a class or an interface inside an interface? As you just saw in the third bullet above about static inner classes, when you define a class or an interface inside an interface, it is implicitly static. So, it is not possible to declare a non-static inner interface! That leaves two possibilities:

class Outer { // an outer class has an inner class class Inner {}

}

97

Chapter 4 advanCed Class design

class Outer { // an outer class has an inner interface interface Inner {}

}

Let’s create a Point class to implement the center of a Circle. Since you want to associate each Circle with a center Point, it is a good idea to make Point an inner class of Circle, as shown in Listing 4-6.

Listing 4-6. Circle.java

public class Circle {

// define Point as an inner class within Circle class class Point {

private int xPos; private int yPos;

//you can provide constructor for an inner class like this public Point(int x, int y) {

xPos = x; yPos = y;

}

//the inner class is like any other class - you can override methods here public String toString() {

return "(" + xPos + "," + yPos + ")";

}

}

// make use of the inner class for declaring a field private Point center;

private int radius;

public Circle(int x, int y, int r) {

// note how to make use of the inner class to instantiate it center = this.new Point(x, y);

radius = r;

}

public String toString() {

return "mid point = " + center + " and radius = " + radius;

}

public static void main(String []s) { System.out.println(new Circle(10, 10, 20));

}

// other methods such as area are elided

}

This implementation of Circle and Point is very similar to what you saw earlier, with the only major difference being that you have defined Point as a private member of Circle here. You are instantiating the inner class like so:

center = this.new Point(x, y);

You might be wondering why you cannot use the usual new statement:

center = new Point(x, y);

98

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