内部类使得多继承的实现变得更加完美,同时也明确了如果父类为抽象类或者具体类,那么就仅能通过内部类来实现多重继承。
实例:儿子是如何利用多重继承来继承父亲和母亲的优良基因。
Father类、Mother类
public class Father {
public int strong(){
return 9;
}
}
public class Mother {
public int kind(){
return 8;
}
}
Son类
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 Test1 {
public static void main(String[] args) {
Son son = new Son();
System.out.println("Son 的Strong:" + son.getStrong());
System.out.println("Son 的kind:" + son.getKind());
}
}
结果:
Son 的Strong:10
Son 的kind:6