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{ }
能帮我解释下么
System.out.println(a1.show(b)); //
System.out.println(a1.show(c)); //A and A,方法中父类型参数可以接受子类型的参数,
System.out.println(a1.show(d)); //直接调用
System.out.println(a2.show(b));
System.out.println(a2.show(c)); //B and A,上转型对象(a2)只能拥有父类已有的方法,但这些方法如果子类重写,则是子类的行为
System.out.println(a2.show(d)); //直接调用
System.out.println(b.show(b));
System.out.println(b.show(c)); //子类对象调用重写的方法
System.out.println(b.show(d)); //子类使用继承的方法
…………
调用类a1里面的方法show 那传递的对象b这个是什么意思?
A a1 = new A();
//创建Class A的对象a1;
A a2 = new B();
//创建Class B的对象并把这个对象看做是它的父类A的一个对象,也就是说虽然a2中包含全部Class B的成员,但是我们只能看见其中Class A所包含的部分;
B b = new B();
//创建Class B的对象b;
C c = new C();
//创建Class C的对象c;
D d = new D();
//创建Class D的对象d;
System.out.println(a1.show(b));
//将对象b作为参数传入a1的show方法,因为对象b是Class A的子类Class B的对象,所以调用的方法是“public String show(A obj)”
System.out.println(a1.show(c));
//同样,因为对象c是Class B的子类Class D的对象,而ClassB又是ClassA的子类,所以同样调用a1形参为(A obj)的方法show
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{ }剩下的就比较好解释了吧,就像前面几位说的,是针对继承和多态的一个联系,主要是判断各个对象的继承关系,和根据继承关系所演变出来的方法调用例子。这个例子说明了,当某一个对象实例存在多个同名方法的时候,根据传入参数的类型不同而调用不同的方法,实际上,如果将“show”方法的形参换成各种不同的基础类型,应该可以更好的理解这个问题。至于多态,我想这个例子还不足以说明多态的问题。
a1.show(b)就是要执行public String show(A obj)这个方法,b是B类对象,B是继承自A类的.所以b也可以当成是A类的对象.就象"白猫"是"猫"一样.
为什么不是执行public String show(D obj)呢,那是因为这个方法要一个D类的参数,D类继承自B类,b不能当成是D类对象,就像"白猫"不能当成"能逮老鼠的白猫"一样.像这样的概念,最好还是自己看看书,把关系理顺,再做题.概念不清,题没有办法做.