java 内部类 静态内部类_Java内部类学习(一)—— 内部类和静态内部类

Static Inner Classes

Consider the following Java code fragment:

public classA

{inty;public static classB

{intx;voidf () {}

}

}

This fragment defines the class A which contains an static inner class B.

A static inner class behaves like any “outer'' class. It may contain methods and fields, and it may be instantiated like this:

A.B object = new A.B ();

This statement creates an new instance of the inner class B. Given such an instance, we can invoke the f method in the usual way:

object.f();

Note, it is not necessarily the case that an instance of the outer class A exists even when we have created an instance of the inner class. Similarly, instantiating the outer class A does not create any instances of the inner class B.

The methods of a static inner class may access all the members (fields or methods) of the inner class but they can access only static members (fields or methods) of the outer class. Thus, f can access the field x, but it cannot access the field y.

理解:1.当我们创建了一个静态内部类实例的时候,并不一定存在一个外部类的实例。同样,当创建了一个外部类实例的时候也没有创建一个静态内部类的实例。2.内部类的方法可以访问内部类的所有变量和方法,但只能访问外部类中的静态变量和静态方法。

Non-Static Inner Classes

By default, an inner class is non-static:

public classA

{inty;public classB

{intx;voidf () {}

}

}

This fragment defines the class A which contains a non-static inner class B.

A non-static inner class can be instantiated only inside a non-static method of the outer class. This is because every instance of a non-static inner class must be associated with an instance of the outer class. In a sense, every instance of a non-static inner class exists ``inside'' an instance of the outer class. A single instance of the outer class may have associated with it more than one instance of the inner class.

Because an instance of a non-static inner class has an associated instance of the outer class, the methods of the inner class can access directly any of the members (fields or methods) of the outer class instance. For example, the f method defined above can access both x and y directly.

The Java keyword this  can be used in a non-static method to refer to the current object instance. Thus in the method f, this refers to an instance of the inner B class. Every non-static inner class is associated with an instance of the outer class. To access the outer class instance inside the method f we write A.this.

理解:1.内部类的实例必须在外部类中的某个成员方法中创建。因为每个内部类实例必须和一个外部类的实例联系在一起。在某种意义上,每一个内部类的实例存在于一个外部类的实例当中。2.内部类的方法可以直接访问外部类中的所有变量和方法(静态和成员)3.this可以用在非静态方法中来指向当前的对象实例。方法f()中,this指向内部类B的实例。A.this指向与这个内部关联的外部类。

下面是overstackflow对内部类和静态内部类之间区别的介绍

Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.

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();

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

classOuterClass {

...classInnerClass {

...

}

}

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();

For completeness note that there is also such a thing as an inner class without an enclosing instance:

classA {int t() { return 1; }static A a = new A() { int t() { return 2; } };

}

Here, new A() { ... } is an inner class defined in a static context and does not have an enclosing instance.

理解:嵌套类分为两种,一种是静态嵌套类,一种是内部类.静态嵌套类和相应的外部类可以相互独立存在,而内部类和相应的外部类是相互依存的.内部类的实例必须由一个外部类实例来创建.

An inner class, by definition, cannot be static, so I am going to recast your question as "What is the difference between static and non-static nested classes?"

A non-static nested class (sometimes incorrectly referred to as an 'inner class') has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.

理解:内部类的实例可以访问相应外部类的所有方法和域.静态嵌套类实例只能访问相应外部类中的静态方法和域.

public classConditionTest2 {private static BoundedBuffer bb = newBoundedBuffer();public static voidmain(String[] args) {//启动10个“写线程”,向BoundedBuffer中不断的写数据(写入0-9);//启动10个“读线程”,从BoundedBuffer中不断的读数据。

for (int i=0; i<10; i++) {new PutThread("p"+i, i).start(); // new TakeThread("t"+i).start();

}

}static classPutThread extends Thread {private intnum;public PutThread(String name, intnum) {

super(name);this.num =num;

}public voidrun() {try{

Thread.sleep(1); //线程休眠1ms

bb.put(num); //向BoundedBuffer中写入数据

} catch(InterruptedException e) {

}

}

}static classTakeThread extends Thread {publicTakeThread(String name) {

super(name);

}public voidrun() {try{

Thread.sleep(10); //线程休眠1ms

Integer num = (Integer)bb.take(); //从BoundedBuffer中取出数据

} catch(InterruptedException e) {

}

}

}

}

上面的代码说明在外部类中可以直接实例化静态嵌套类,而不需要加外部类的名字.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值