内部类实现多重继承

内部类的优点:

1、内部类可以访问外部类的私有数据。

2、内部类具有封装性。

3、匿名类可以实现“多重继承”。

4、匿名内部类可以支持回调。

参考https://blog.csdn.net/weixin_44234912/article/details/108798787

public class Father {
    public int strong() {   
        // 强壮指数
        return 9;
    }
}
public class Mother {
    public int kind() {    
        // 友好指数
        return 8;
    }
}
public class Son {
    // 内部类继承Father类
    class Father_1 extends Father {
        public int strong() {
            return super.strong() + 1;
        }
    }
    class Mother_1 extends Mother {
        public int kind() {
            return super.kind() - 2;
        }
    }
    public int getStrong() {
        return new Father_1().strong();
    }
    public int getKind() {
        return new Mother_1().kind();
    }
}
public class Test {
    public static void main(String[] args) {
        Son son = new Son();
        System.out.println("Son 的强壮指数:" + son.getStrong());
        System.out.println("Son 的友好指数:" + son.getKind());
    }
}
Son 的强壮指数:10
Son 的友好指数:6

 

posted @ 2022-09-23 15:18  江境纣州  阅读(53)  评论(0编辑  收藏  举报