什么是内部类?内部类就是一个类的内部再去定义一个类,这个类作为外部的一个属性存在。其中,内部类作为外部类的一个成员可以自由的访问外部类中的属性。而外部类不可以直接访问内部类的属性
内部类有四种存在方式:成员内部类、静态内部类、局部内部类、匿名内部类。
成员内部类:成员内部类是类作为外部类的成员而存在,不能带有关键static,内部类在外部类看来相当于一个属性或方法。同样内部类可以用public、private等权限修饰符修饰。内部类中同样可以定义自己的属性以及方法,内部类通过关键字this来访问自己的属性以及方法,通过外部类.this.属性或方法来访问外部类的属性以及方法。
成员内部类有三种实例化方法,分别为:1、通过外部类的非静态方法来实例化内部类。2、通过外部类的静态方法实例化内部类。3、内部类中通过this来表示内部类的实例。
先建立外部类对象再 外部类类名.内部类类名 对象名=外部类对象.new 内部类(); 或者两部化一步: 外部类类名.内部类类名 对象名=new 外部类类名().new 外部类()
package innter_class;
import org.omg.Messaging.SyncScopeHelper;
public class Outer1 {
private static String name;
public Outer1() {
name="name";
//Inter1 inter1=new Inter1();
}
//外部类的非静态方法访问成员内部类
public void instanceInner1() {
Inter1 inter1=new Inter1();
inter1.displayInter1();
}
//外部类的静态方法访问成员内部类
public void staticIntanceInner1() {
Inter1 staticInter1=new Inter1();
staticInter1.displayInter1();
}
class Inter1{
//static int inter_i=100;
//成员内部类中不允许定义静态成员
int inter_i=100;
public void displayInter1() {
System.out.println(Outer1.this.name+this.inter_i);
}
}
public static void main(String[] args) {
Outer1 outer1=new Outer1();
outer1.instanceInner1();
outer1.staticIntanceInner1();
//外部类的静态方法访问内部类
Outer1.Inter1 in=outer1.new Inter1();//根据外部类对象创建内部类对象
in.displayInter1();//访问内部类方法
}
}
静态内部类:用static修饰的内部类叫做静态内部类。注意:一个内部类如果用static修饰就相当于这个内部类是一个外部定义的类。static修饰的内部类可以在内部定义属于内部类的static属性,不用static修饰的普通内部类不能定义用static修饰的属性和方法。而静态内部类只能访问外部类的静态属性和方法。静态内部类的实例化不必像普通内部类一样先实例化外部类再实例化内部类,可以直接实例化内部类,如:外部类类名.内部类类名 对象名=new 外部类类名.内部类类名()。
package innter_class;
public class includeStaticInterOuter {
static class Inter{
static int inter_i=100;
String name="李四";
public void printInter() {
System.out.println(inter_i+name);
}
}
//普通方法创建内部类
public void setInter() {
Inter inter=new Inter();
inter.printInter();
}
//静态方法创建内部类对象
public static void setInter1() {
Inter inter=new Inter();
inter.printInter();
System.out.println(Inter.inter_i);//外部类可以访问内部类中静态属性
//System.out.println(Inter.name);
}
public static void main(String args[]){
includeStaticInterOuter outer=new includeStaticInterOuter();
outer.setInter();
outer.setInter1();
//静态内部类的创建
includeStaticInterOuter.Inter inter=new includeStaticInterOuter.Inter();
inter.printInter();
}
}
局部内部类:局部内部类也叫方法内部类,即在一个方法内部定义的内部类。内部类的实例化过程和普通内部类相似,必须先实例化外部类再进行内部类的实例化。package innter_class;
public class includelocalInterclassOuter {
private int i=100;
public void display() {
int it=10;
class localInter{
private int i=1;
public localInter() {
doInter();
}
public void doInter() {
System.out.println(includelocalInterclassOuter.this.i);
System.out.println(it);
System.out.println(this.i);
}
}
//localInter localInter=new localInter();
//localInter.doInter();
new localInter();
}
public static void main(String args[]) {
includelocalInterclassOuter includelocalInterclassOuter=new includelocalInterclassOuter();
includelocalInterclassOuter.display();
}
}
匿名内部类:匿名内部类就是没有名字的内部类,正因为它是没有名字的,所以匿名内部类是没有构造器的。
注意:匿名内部类中不能定义任何静态成员、方法和类,并且不能用public、protected、private、static等修饰,只能创建匿名内部类的一个实例。一个匿名内部类一定是在new的后面,用其隐含的实现一个接口或扩展一个类。
下面为普通内部类实例与匿名内部类实例对比
package innter_class;
public interface A {
public void funA();
}
package innter_class;
public class AnonymousOuter {
public void callOuter(A a) {
a.funA();
}
public static void main(String[] args) {
class Inter implements A{
public void funA() {
System.out.println("普通内部类");
}
}
new AnonymousOuter().callOuter(new Inter());
}
}
package innter_class;
public class includeAnonymousInterClassOuter {
public void funOuter(A a) {
a.funA();
}
public static void main(String[] args) {
new includeAnonymousInterClassOuter().funOuter(new A()
{
public void funA() {
System.out.println("匿名内部类");
}
});
}
}