public class Polymorphism {public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b));
System.out.println(a1.show(c));
System.out.println(a1.show(d));
System.out.println(a2.show(b));
System.out.println(a2.show(c));
System.out.println(a2.show(d));
System.out.println(b.show(b));
System.out.println(b.show(c));
System.out.println(b.show(d));
}
}class A{
public String show(D obj){
return("A and D");
}
public String show(A obj){
return("A and A");
}
}class B extends A{
public String show(B obj){
return("B and B");
}
public String show(A obj){
return("B and A");
}
}class C extends B{}class D extends B{}
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b));
System.out.println(a1.show(c));
System.out.println(a1.show(d));
System.out.println(a2.show(b));
System.out.println(a2.show(c));
System.out.println(a2.show(d));
System.out.println(b.show(b));
System.out.println(b.show(c));
System.out.println(b.show(d));
}
}class A{
public String show(D obj){
return("A and D");
}
public String show(A obj){
return("A and A");
}
}class B extends A{
public String show(B obj){
return("B and B");
}
public String show(A obj){
return("B and A");
}
}class C extends B{}class D extends B{}
解决方案 »
- 关于像mysql插入数据时防止插入重复
- 数据结构
- java如何实现c结构体的对齐?
- JOptionPane.showInputDialog覆盖的问题???
- java运行的必备条件是?
- 与大家分享一些好资源(免费电子书和视频教程)
- 我用的是 MyEclipse5.0 ,在它里面运行很好的Java类:MyExcel.java,放在dos下面用 java MyExcel 时就报错,这是为什么啊?
- 我的机器编译java为什么这么慢?解决问题者得30分.
- 求高手点拨一个CS结构聊天界面的UI代码实现问题所在....
- java内存区域分配疑问
- 每日一题(15)
- 关于Collections.sort的两个小问题
看看这个
所以
System.out.println(a1.show(b)); //调用A的show(A a)方法 //调用a1实例的方法
System.out.println(a1.show(c)); //调用A的show(A a)方法
System.out.println(a1.show(d)); //调用A的show(D d)方法 //匹配最精确的方法
System.out.println(a2.show(b)); //调用B的show(B b)方法 //匹配最精确
System.out.println(a2.show(c)); //调用B的show(A a)方法 //调用a2实例(也就是B对象)的方法
System.out.println(a2.show(d)); //调用A的show(D d)方法 //B继承A,所以B有show(D d)方法,匹配最精确
System.out.println(b.show(b)); //调用B的show(B b)方法
System.out.println(b.show(c)); //调用B的show(A a)方法 //b实例也就是B对象的方法
System.out.println(b.show(d)); //调用A的show(D d)方法 //B继承A,所以B有show(D d)方法,匹配最精确
System.out.println(a1.show(c));//同上,不可能匹配D,只能是A
System.out.println(a1.show(d));//匹配到D了,调用A的show(D obj)
System.out.println(a2.show(b));//BA,没想明白
System.out.println(a2.show(c));//BA,没想明白
System.out.println(a2.show(d));//A的show(D obj),B中未重写该函数,找到继承下来的最匹配的D
System.out.println(b.show(b));//B中重写的最匹配的show(B obj)
System.out.println(b.show(c));//C类型向上找,B中重写的最匹配的show(B obj)
System.out.println(b.show(d));//同a2调用
A and A
A and D
B and B
B and B
B and B
B and B
B and B
B and B 这样嘛?
A and D
A and D
B and B
B and B
A and D
B and B
B and B
A and D
A and A
A and A
A and D
B and B
B and B
A and D
B and B
B and B
A and D
System.out.println(a2.show(b));//这个只会找A中的方法,如果B重写了则找B的,这里找到最匹配的B中的show(B)
System.out.println(a2.show(c));//同上面的,从C类型往上找找到最匹配的B类型的
public class Polymorphism {public static void main(String[] args) {
A a = new A();
A b = new B();
A c = new C();
A d = new D();
System.out.println(b.show(a));
System.out.println(c.show(a));
System.out.println(d.show(a));
}
}class A{
public String show(A obj){
return("A and A");
}
}class B extends A{
public String show(A obj){
return("B and B");
}
}class C extends A{
public String show(A obj){
return("C and C");
}
}class D extends A{
public String show(A obj){
return("D and D");
}
}
而且看你上面说的楼主只有一处用到多态,其实还有些你没有看到
public String show(B obj){
return("B and B");
}
public String show(A obj){
return("B and A");
}
像这些方法传了一个A的子类或者B的子类的对象,也叫多态
糊涂了,忽略了一些细节,只考虑运行期,没有考虑编译期了System.out.println(a1.show(b)); //调用A的show(A a)方法 //调用a1实例的方法
System.out.println(a1.show(c)); //调用A的show(A a)方法
System.out.println(a1.show(d)); //调用A的show(D d)方法 //匹配最精确的方法System.out.println(a2.show(b)); //调用B的show(A a)方法 //编译期a2是A类型,所以编译器会查找A类最精确匹配的方法show(A a),运行期,a2指向B实例,所以实际调用B实例的show(A a)方法
System.out.println(a2.show(c)); //调用B的show(A a)方法 //理由同上System.out.println(a2.show(d)); //调用A的show(D d)方法 //编译期决定调用方法是A的show(D d),运行期调用B实例的方法,B继承A,所以B有show(D d)方法
System.out.println(b.show(b)); //调用B的show(B b)方法System.out.println(b.show(c)); //调用B的show(B b)方法 //b是B实例,参数也是c也是B的子类,匹配最精确方法System.out.println(b.show(d)); //调用A的show(D d)方法 //B继承A,所以B有show(D d)方法,匹配最精确