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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM