重新来认识一下内部类的区别
// just like static method, static member class has public/private/default access privilege levels
// access privilege level: public
public static class Inner1 {
public Inner1() {
// Static member inner class can access static method of outer class
staticMethod();
// Compile error: static member inner class can not access instance method of outer class
// instanceMethod();
}
}
// access privilege level: default
static class Inner2 {
}
// access privilege level: private
private static class Inner3 {
// define a nested inner class in another inner class
public static class Inner4 {
}
}
private static void staticMethod() {
// cannot define an inner class in a method
/* public static class Inner4() {
} */
}
private void instanceMethod() {
// private static member class can be accessed only in its outer class definition scope
Inner3 inner3 = new Inner3();
// how to use nested inner class
Inner3.Inner4 inner4 = new Inner3.Inner4();
}
}
class Test {
Outer.Inner1 inner1 = new Outer.Inner1();
// Test and Outer are in the same package, so Inner2 can be accessed here
Outer.Inner2 inner2 = new Outer.Inner2();
// Compile error: Inner3 cannot be accessed here
// Outer.Inner3 inner3 = new Outer.Inner3();
}
静态成员类可访问外部类的任一静态字段或静态方法
像静态方法或静态字段一样,静态成员类有public/private/default权限修饰符
静态成员类不能与外部类重名
像外部类的静态方法一样,不能直接访问外部类的实例字段和实例方法
静态成员类只能定义于外部类的顶层代码或外部类其它静态成员类的顶层代码中(嵌套定义);不能定义于外部类的某个函数中。
…;
private static class Entry < E > {
E element;
Entry < E > next;
Entry < E > previous;
Entry(E element, Entry < E > next, Entry < E > previous) {
this .element = element;
this .next = next;
this .previous = previous;
}
}
…;
}
// just like instance method, member class has public/private/default access privilege levels
private int data;
// access privilege level: public
public class Inner1 {
private int data;
private int data1;
public Inner1() {
// member class can access its outer class' instance field directly
data1 = 1 ;
// itself data field
data = 1 ;
// its outer class instance field
Outer. this .data = 1 ;
}
}
// access privilege level: default
class Inner2 {
// can not define static filed, method, class in member class
// static int j = 1;
// but, "static final" compound is allowed
static final int CONSTANT = 1 ;
}
// access privilege level: private
private class Inner3 {
public class Inner4 {
}
}
// in fact, Inner5 is not a member class but a static member class
interface Inner5 {
}
private static void staticMethod() {
// can not create a member class instance directly in outer class' static method
// Inner1 inner1 = new Inner1();
}
private void instanceMethod() {
// can create a member class instance in outer class' instance method
Inner1 inner1 = new Inner1();
}
}
class Test {
public Test() {
// cannot create member class instance directly in class other than outer class
// Outer.Inner2 inner2 = new Outer.Inner2();
// create a member class instance outside it's outer class
Outer outer = new Outer();
Outer.Inner1 inner1 = outer. new Inner1();
}
}
成员类不能与外部类重名
不能在成员类中定义static字段、方法和类(static final形式的常量定义除外)。因为一个成员类实例必然与一个外部类实例关联,这个static定义完全可以移到其外部类中去
成员类不能是接口(interface)。因为成员类必须能被某个外部类实例实例化,而接口是不能实例化的。事实上,如示例代码所示,如果你以成员 类的形式定义一个接口,该接口实际上是一个静态成员类,static关键字对inner interface是内含(implicit)的。
|
OuterClass.InnerClass innerClass = outerClass.new InnerClass();
|
private class Itr implements Iterator < E > {
………;
}
public Iterator < E > iterator() {
return new Itr();
}
}
private int instanceField;
private static int staticField;
// define a local member class in instance code block
{
int localVirable1 = 0 ;
final int localVirable2 = 1 ;
class Inner1 {
public Inner1() {
// can access its outer class' field and method directly
instanceField = 1 ;
// use OuterClass.this to get its corresponding outer class instance
Outer. this .instanceField = 1 ;
// can not access the not final local virable in its containing code block
// System.out.print(localVirable1);
// can access the final local virable in its containing code block
System.out.print(localVirable2);
}
}
// local class can not have privilege modifier
/* public class inner2 {
} */
}
// define a local static member class in static code block
static {
class Inner2 {
public Inner2() {
staticField = 1 ;
// can not access instance field and method in a local static member class
// intanceField = 2;
}
}
}
public void intanceMethod() {
// define a local class in its out class' instance method
class Inner3 {
}
// local class is visible only in its containning code block
// Outer.Inner2 inner2;
}
private static void staticMethod() {
// define a local static member class in its out class' static method
class Inner4 {
public Inner4() {
staticField = 2 ;
}
}
// can not define a interface as a local class
/* interface I {
} */
}
}
如示例代码所示,内部类只在定义它的代码段中可见,不能在它所属代码段之外的代码中使用;因此也就没有public/private/default权限修饰符(无意义)
不能以局部类形式定义一个接口。局部类只在其所属代码段中可见,定义这样的接口无意义
局部类类名不能与其外部类类名重复
public void instanceMethod() {
// define a nonymous class which implements Action interface and creat an instance of it
Action action = new Action() {
public void doAction() {
System.out.println( " a simple anonymous class demo " );
}};
action.doAction();
// define a nonoymous class which extends BaseClass and create an instance of it
new BaseClass( 5 ) {
public void printData(){
System.out.println( " data = " + getData());
}
}.printData(); // "data = 5" will be outputed
}
}
interface Action {
void doAction();
}
class BaseClass {
private int data;
public BaseClass ( int data) {
this .data = data;
}
public int getData() {
return data;
}
}
new class-name ( [ argument-list ] ) { class-body }
|
new interface-name () { class-body }
|
该类定义代码段很短
只需要创建该类的一个实例
类的定义代码与类的使用代码紧邻
使用匿名不影响代码的易读性
// Now call the list() method with a single FilenameFilter argument
// Define and instantiate an anonymous implementation of FilenameFilter
// as part of the method invocation expression.
String[] filelist = f.list( new FilenameFilter() {
public boolean accept(File f, String s) { return s.endsWith( " .java " ); }
}); // Don't forget the parenthesis and semicolon that end the method call!
本文深入解析Java内部类的四种类型:静态成员类、成员类、局部类和匿名类的特点、约束及应用场景,辅以示例代码帮助理解。
3102

被折叠的 条评论
为什么被折叠?



