Java内部类

内部类

概述

  • 内部类 如其名,就是类的内部定义的一个类
  • 内部类访问特点
    • 内部类可以直接访问外部类的成员
    • 外部类要访问内部类,必须建立内部类的对象
  • 一般用于类的设计
  • 分析事物时,发现该事物描述中还有事物,而且这个事物还在访问被描述的事物,这时就把含有的事物定义成内部类来描述

修饰符

  • 如何直接访问外部类中的内部类的成员
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class Outer {
    private int num = 3;
    public class Inner {
    void show(){
    System.out.println("num = "+num);
    }
    }
    }
    public class Test {
    public static void main(String[] args){
    Outer.Inner in = new Outer().new Inner();
    in.show();
    }
    }

static

  • 如果内部类是静态的,该如何访问内部类方法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class Outer {
    private int num = 3;
    public static class Inner {
    void show(){
    System.out.println("num = "+num);
    }
    //如果内部类中定义了静态成员,该内部类必须也是静态的
    static void fun(){
    System.out.println("fun");
    }
    }
    }
    public class Test {
    public static void main(String[] args){
    //如果内部类是静态的,相当于一个外部类
    Outer.Inner in = new Outer.Inner();
    in.show();
    //如果内部类是静态的,成员是静态的
    Outer.Inner.fun();
    }
    }

细节

  • 观察如下代码判断运行结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class Outer {
    private int num = 3;
    public class Inner {
    int num = 4;
    public void show(){
    int num = 5;
    System.out.println(num);
    System.out.println(this.num);
    System.out.println(Inner.this.num);
    System.out.println(Outer.this.num);
    }
    }
    public void method(){
    new Inner().show();
    }
    }
    public class Test {
    public static void main(String[] args){
    new Outer.method();
    }
    }
  • 结果是:5 4 4 3

  • 为什么内部类能直接访问外部类中成员那?
  • 那是因为内部类持有了外部类的引用。 外部类名.this

局部内部类

  • 内部类可以存放在局部位置上
  • 内部类在局部变量位置上只能访问局部中被final修饰的局部变量
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public class Outer {
    private int num = 3;
    public void method(){
    //如果不加final,会报错
    final int x = 9;
    public class Inner {
    public void show(){
    System.out.println(x);
    }
    }
    Inner in = new Inner();
    in.show();
    }
    }
    public class Test {
    public static void main(String[] args){
    new Outer.method();
    }
    }

匿名内部类

  • 匿名内部类,就是内部类的简写格式
  • 前提:内部类必须继承或实现一个外部类或接口
  • 匿名用户类:其实就是一个用户匿名子对象
  • 格式:new 父类or接口 (){ 子类内容
  • 通常使用场景之一:
  • 当函数参数是接口类型时,而且接口中的方法不超过三个,可以用匿名内部类作为实际参数进行传递
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    interface inter{
    public void show1();
    public void show1();
    }
    class Outer{
    public void method(){
    /*可以这么写
    new inter(){
    public void show1(){}
    public void show1(){}
    }*/
    //也可以这么写
    inter in = new inter(){
    public void show1(){}
    public void show1(){}
    }
    /*居然还可以这么写
    new inter(){
    public void show1(){}
    public void show1(){}
    }.show1();
    */
    }
    }

小细节

静态问题

  • 试试能不能编译如下代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class InnerClassDemo {
    class Inner{

    }
    public static void main(String[] args){
    new Inner();
    }
    public void method(){
    new Inner();
    }
    }
  • ‘InnerClassDemo.this’ cannot be referenced from a static context

  • 为什么 method 也写了同样的内容却没有报错?
  • 因为 main 是静态的,只能直接调用静态成员

转型问题

  • 运行如下代码,将注释1取消注释,观察结果,再注释掉1,取消掉注释2,观察结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    public class Outer {
    public void method(){
    /*注释1
    new Object(){
    public void show(){
    System.out.println("i can run");
    }
    }.show();
    */
    /*注释2
    Object obj = new Object(){
    public void show(){
    System.out.println("i can run");
    }
    };
    obj.show();
    */
    }
    }
    public class Test {
    public static void main(String[] args){
    new Outer.method();
    }
    }
  • 为什么第一次编译运行正确,第二次编译错误?

  • 因为匿名内部类这个子类对象被向上转型为 object 类型,这样就不能再使用子类特有的方法了