Java中的内部类

将一个类的定义放在另一个类的定义内部,成为内部类。

一、各种形式的内部类及其基本使用

成员内部类:作为外部类的一个成员存在,与外部类的属性、方法并列的类。

class Outer{
    private int field = 1;
    private int anotherField = 2;

    //成员内部类
    public class Inner{
        private int field = 10;

        //成员内部类不能有静态变量与方法,否则出现编译错误:
        //  Inner classes cannot have static declarations
        //private static String staticField = "static";

        public void func(){
            //内部类拥有外部类的所有元素的访问权,
            //也可以用Outer.this访问外部类对象
            System.out.println("Inner field: " + field);
            System.out.println("Outer field: " + Outer.this.field);
            System.out.println("Outer anotherField: " + anotherField);
        }
    }
}

public class InnerClassTest {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //需要通过外部类对象创建成员内部类对象
        Outer.Inner inner = outer.new Inner();
        inner.func();
    }
}

 输出

Inner field: 10
Outer field: 1
Outer anotherField: 2

 

静态内部类:定义在一个类内部的static的类。

class Outer{
    //静态内部类不能访问外部类的非静态成员
    private int field = 1;
    private static int STATIC_FIELD = 2;

    //静态内部类
    public static class Inner{
        private int field = 10;
        private static int STATIC_FIELD = 20;

        public void func(){
            System.out.println("Inner field: " + field);
            System.out.println("Inner static field: " + STATIC_FIELD);
            System.out.println("Outer static field: " + Outer.STATIC_FIELD);
        }
    }
}

public class InnerClassTest {
    public static void main(String[] args) {
        //直接使用new创建静态内部类
        Outer.Inner inner = new Outer.Inner();
        inner.func();
    }
}

 输出

Inner field: 10
Inner static field: 20
Outer static field: 2

 

 局部内部类:定义在方法或者某个作用域内的类。

interface Interface {
    public void func();
}

class Outer {
    public Interface getInner(int field) {
        //局部内部类
        class Inner implements Interface {
            private int field;

            public Inner(int field) {
                this.field = field;
            }

            @Override
            public void func() {
                System.out.println("Inner: " + field);
            }
        }

        return new Inner(field);
    }

    public void accessInner() {
        //不能在其作用域外访问局部内部类
        //编译错误:Cannot resolve symbol 'Inner'
        Inner inner = new Inner(1);
    }
}

public class InnerClassTest {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Interface inner = outer.getInner(3);
        inner.func();
    }
}

 

匿名内部类

abstract class AbstractClass {
    protected static int STATIC_FIELD;

    public AbstractClass(int field) {
        this.STATIC_FIELD = field;
    }

    public abstract void func();
}

class Outer {
    public AbstractClass getAnonymousClass(int field) {
        String localField = "localField";

        //创建并返回匿名内部类
        return new AbstractClass(field) {
            @Override
            public void func() {
                System.out.println(STATIC_FIELD);
                //如果在匿名内部类中要使用外部方法的局部变量或者参数变量,
                //则他们必须是final的,下面两句会产生编译错误:
                //Variable 'field' is accessed from within inner class.
                //Needs to be declared final.
                System.out.println(field);
                System.out.println(localField);
            }
        };
    }
}

public class InnerClassTest {
    public static void main(String[] args) {
        Outer outer = new Outer();
        AbstractClass anonymousClass = outer.getAnonymousClass(2);
        anonymousClass.func();
    }
}

 

二、继承内部类

class Outer {
    class Inner {
        private int field;

        public Inner(int field) {
            this.field = field;
        }
    }
}

class InheritInner extends Outer.Inner {
    // 继承成员内部类必须传入一个外部类对象并显式地初始化其外
    // 部类对象的引用,否则该类会出现编译错误:
    // No enclosing instance of type 'Outer' is in scope
    public InheritInner(Outer outer, int field) {
        outer.super(field);
    }
}

 

三、多态与内部类

class SuperOuter{
    class Inner{
        public void func(){
            System.out.println("SuperOuter.Inner");
        }
    }
}

class SubOuter extends SuperOuter{
    //内部类不具有多态性,因此这个内部类不会覆盖父类的同名内部类,
    //两个内部类是存在于不同命名空间的独立的类
    class Inner{
        public void func(){
            System.out.println("SubOuter.Inner");
        }
    }
}

public class InnerClassTest {
    public static void main(String[] args) {
        SuperOuter outer = new SubOuter();
        //下面这句会调用SuperOuter.Inner的方法
        outer.new Inner().func();
    }
}

 输出

SuperOuter.Inner

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值