内部类
不一定在类的内部,方法的内部也是可以的;应用的场景不是很多;只需能看懂别人的代码;和Swing应用;
四种内部类
1、
静态内部类(static inner class):只能访问外部类的静态成员变量与静态方法,生成静态内部类对象的方式为:
OuterClass.InnerClass inner = new OuterClass.InnerClass();
The simplest form of inner class
uCan’t have the same name as the enclosing class
uCompiled into a completely separate .class file from the outer class
uCan access only static members and methods of the enclosing class, including private static members
uCreate an instance of a static inner class out of enclosing class:
上面是四个要求
是由Static关键字修饰的
只能访问外部类的静态变量和静态方法;
2、成员内部类
Defined in an enclosing class without using the static modifier
uLike instance variables
uCan access all members of the enclosing class
uCreate an instance within the enclosing classthis.newInnerclass();
uCreate an instance out of the enclosing class(new Outerclass()).new Innerclass();
uAccess members of the enclosing class within inner classesOuterclass
类比非静态元素;有Static关键字就是
成员内部类:
3、局部内部类
uDefined within the scope of a method, even smaller blocks within methods
uThe least used form of inner class
uLike local variables, can’t be declared public, protected, private and static
uCan only access final local variables
不能被外部的使用;
4、匿名内部类
Local inner classes which don’t have class names
uNo key word class
uNo key word extends and implements
uNo constructors
uImplicitly(隐士) extend a superclass or implement an interface
匿名内部类(Anonymous Inner Class):匿名内部类会隐式地继承一个父类或实现一个接口