问题
In java what are nested classes and what do they do?
回答1:
They're just classes within other classes. They make it possible to have a hierarchy of classes, and if you make them private they're a convenient way to encapsulate data that isn't exposed outside of the class using them. Sun has a short tutorial about them
回答2:
One of the most important uses of inner classes in Java are listeners. Instead of writing an entire separate class for an ActionListener
or similar, you create it in-place:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
... your code here ...
}
}
These inner classes typically call functions in the outer class (they capture a pointer to the object they were created in), and lead to much tidier code than having a single callback with some logic that has to figure out which button etc. was clicked. You also don't have to modify code in several places when you add or remove a button, since the inner class is created where you create the button. It's quite elegant.
回答3:
Nested classes are classes that are declared within classes, but have the modifier static
. They are a way of organizing the class so that it has access to the private declarations of its outer class, and can be used to better indicate the relationship between the outer and nested class.
They are different from inner classes (declared the same way but without the static) in that an inner class has to have an instance of the outer class as a reference to be able to be instantiated. A nested class, by contrast, isn't tied to a specific instance of the outer class.
EDIT: In response to those who say that a "nested class" can be both a static nested class or an inner class. OK, but you would never call an inner class a nested class, would you? Sure that isn't by the spec (A nested class is any class whose declaration occurs within the body of another class or interface.
) But it is how it is used in practice.
回答4:
A Nested Class is a class defined within another class. Sun docs indicate you use them because:
- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.
There are 2 types: static nested classes and inner classes.
A static nested class is a nested class that has been declared static. It is syntactic sugar for what would have been a tightly-coupled top-level class to allow it to be "stashed" within its owning class to indicate its strong relationship to the other class. However, static nested classes don't have any special behaviors or privileges at run-time - its a packaging convenience to achieve the benefits listed above.
An inner class is a nested class that has not been declared static. Unlike a static nested class, inner classes have special rules/behavior that only apply to inner classes. Specifically, an instance of an inner class can only exist in the context of an instance of the outer class, i.e. each inner class instance is tied to a specific outer class instance. An inner class instance has special access to its related outer class instance. This allows for a 'friend'-like access that you might be familiar with in C
.
There are plenty of documentation on the subject to learn more, but hopefully that gets you started. Nest logical step is Sun's tutorial on Nested Classes.
来源:https://stackoverflow.com/questions/2634239/in-java-what-are-nested-classes-and-what-do-they-do