java用内部类实现多重继承

一般我们都知道java是不支持多重继承的,但其实可以用间接的方法内部类来实现多重继承。以下是代码:

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();
    }
}

 

posted @ 2019-06-27 16:20  志不坚者智不达  阅读(1005)  评论(0编辑  收藏  举报