28.成员内部类 静态内部类 局部内部类 匿名内部类
内部类的访问规则:
1.内部类可以直接访问外部类中的成员,包括私有
之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式:外部类名.this
2.外部类要访问内部类,必须建立内部类对象
访问格式:
1.当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中直接建立内部类对象。
格式:
外部类名.内部类名 变量名=外部类对象.内部类对象
Outer.Inner in=new Outer().new Inner();
2.当内部类在成员位置上,就可以被成员修饰符所修饰。
比如,private:将内部类在外部类中进行封装
static:内部类就具备了静态的特性
当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。
在外部其他类中,如何直接访问静态内部类的非静态成员呢?
new Outer.Inner().function();
在外部其他类中,如何直接访问静态内部类的静态成员呢?
Outer.Inner.function();
注意:当内部类中定义了静态成员,该内部类必须是静态的
class Inner{ //不能 [成员静态,类不是静态],这种写法错误
static void function(){
System.out.println("inner:");
}
}
当外部类中的静态方法访问内部类时,内部类也必须是静态的
当描述事物时,事物的内部还有事物,该事物用内部类来描述
因为内部事物在使用外部事物中的内容。
class Body{
private class xinZang{
}
public void show(){
new xinZang()
}
}
public class NeiBuLei_01 { public static void main(String[] args) { //Outer out=new Outer(); //out.method(); //直接访问内部类中的成员(成员内部类) /*Outer.Inner in=new Outer().new Inner(); in.function();*/ //访问静态内部类中的非静态成员 /*new Outer.Inner().function();*/ //访问静态内部类中的静态成员 /*Outer.Inner.function();*/ //Outer.method1(); } } class Outer{ //private static int x=3; private static int x=3; /*class Inner{ //内部类 int x=6; void function(){ int x=4; System.out.println("inner:"+x); //x:x=4 this.x:x=6 Outer.this.x:x=3 } }*/ /*static class Inner{ //内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限 static void function(){ System.out.println("inner:"+x); } }*/ /*static class Inner2{ void show(){ System.out.println("inner2 show"); } } public static void method1(){ //Inner.function(); //正确 new Inner2().show(); //需要Inner2为static }*/ void method(){ //外部类访问内部类(成员内部类) System.out.println(x); /*Inner in=new Inner(); in.function(); */ } }
局部内部类
内部类定义在局部时:(局部内部类)
1.局部内部类不能用static修饰,static仅修饰成员
2.可以直接访问外部类中的成员,因为还有外部类中的引用
但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量。
public class NeiBuLei_02 { public static void main(String[] args) { // TODO Auto-generated method stub new Outer1().method(7); new Outer1().method(8); } } class Outer1{ int x=3; void method(final int a){ final int y=4; class Inner{ void function(){ System.out.println("inner:"+Outer1.this.x); System.out.println(y+"--"+a); } } new Inner().function(); } }
匿名内部类
匿名内部类:
1.匿名内部类其实就是内部类的简写格式
2.定义匿名内部类的前提:
内部类必须是继承一个类或者实现接口
3.匿名内部类的格式:
new 父类or接口(){定义子类的内容}.方法();
4.其实匿名内部类就是一个匿名子类的对象,而且这个对象有点胖。可以理解为带内容的对象
5.匿名内部类中的方法最好不要超过3个
public class NeiBuLei_03 { public static void main(String[] args) { // TODO Auto-generated method stub new Outer2().function(); } } abstract class AbsDemo{ abstract void show(); } class Outer2{ int x=3; /*class Inner extends AbsDemo{ void show(){ System.out.println("show:"+x); } void abc(){ System.out.println("haha"); } }*/ public void function(){ //new Inner().show(); //AbsDemo的匿名子类对象 new AbsDemo(){ void show(){ System.out.println("show:"+x); } //可以写多个方法 void abc(){ System.out.println("haha"); } }.show(); //.show()可以换为.abc() } }
使用匿名内部类补足代码
public class NeiBuLei_04 { public static void main(String[] args) { // TODO Auto-generated method stub Test.function().method(); /* Test.function():Test类中有一个 静态的方法function .method():function这个方法运算后的结果是一个Inter类型的对象 因为只有Inter类型的对象才可以调用method方法 */ } } interface Inter2{ public abstract void method(); } abstract class Test{ /*static class SubInter2 implements Inter2{ public void method(){ System.out.println("method run"); } }*/ //补足代码,通过匿名内部类 static Inter2 function(){ //return new SubInter2(); return new Inter2(){ public void method(){ System.out.println("method run"); } }; } }