Nested Classes,Static Classes,Non-static Classes,Inner Classes,Local Classes,Anonymous Classes

来源:互联网 发布:初开淘宝店卖什么好 编辑:程序博客网 时间:2024/05/21 07:14

Nested Classes 嵌套类,包括Static Classes静态类和Non-static Classes 也就是Inner Classes内部类。

Inner Classes通常指在一个类(外部类)中直接定义的内部类。Inner Classes又有两种特殊情况,即Local Classes,Anonymous Classes。

Local Classes指在外部类的一个方法中定义的内部类。

Anonymous Classes和Local Classes很像除了没有名字外。


以下内容来自官网文档:

Nested Classes

The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:

class OuterClass {    ...    class NestedClass {        ...    }}

Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declaredstatic are called static nested classes. Non-static nested classes are called inner classes.
class OuterClass {    ...    static class StaticNestedClass {        ...    }    class InnerClass {        ...    }}

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared privatepublicprotected, or package private. (Recall that outer classes can only be declared public or package private.)

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.


Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =     new OuterClass.StaticNestedClass();

在静态嵌套类内部,不能访问外部类的非静态成员,这是由Java语法中"静态方法不能直接访问非静态成员"所限定。若想访问外部类的变量,必须通过其它方法解决,由于这个原因,静态嵌套类使用很少。


Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {    ...    class InnerClass {        ...    }}

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

There are two special kinds of inner classes: local classes and anonymous classes.


本文主要是方便以后自己查阅方便,若要全面学习嵌套类,请到官网下载或查看文档  http://docs.oracle.com/javase/tutorial




0 0
原创粉丝点击