class A {
public String show(D obj) {
return (" A and D");
}
public String show(A obj) {
System.out.println((this instanceof A) + " A this instanceof A");
System.out.println((this instanceof B) + " A this instanceof B");
return (" A and A");
}
}
class B extends A {
public String show(B obj) {
return (" B and B");
}
public String show(A obj) {
System.out.println((this instanceof A) + " B this instanceof A");
System.out.println((this instanceof B) + " B this instanceof B");
return (" B and A");
}
}
class C extends B {
}
class D extends B {
}
public class Test {
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("1--" + a1.show(b));
System.out.println("2--" + a1.show(c));
System.out.println("3--" + a1.show(d));
System.out.println("4--" + a2.show(b));
System.out.println("5--" + a2.show(c));
System.out.println("6--" + a2.show(d));
System.out.println("7--" + b.show(b));
System.out.println("8--" + b.show(c));
//->B.show(C obj)->super.show(C obj)->B.show((super)C obj)->B.show(B obj)=b.show(b)
System.out.println("9--" + b.show(d));
//->B.show(D obj)->super.show(D obj)=A.show(D obj)
}
}
运行结果为
true A this instanceof A
false A this instanceof B
1-- A and A
true A this instanceof A
false A this instanceof B
2-- A and A
3-- A and D
true B this instanceof A
true B this instanceof B
4-- B and A
true B this instanceof A
true B this instanceof B
5-- B and A
6-- A and D
7-- B and B
8-- B and B
9-- A and D
这是一个多态经典例题
1
多态的两种体现是方法的重载与覆写,对象的多态性,
对象的多态性主要分为两种类型,向上转型,子类对象->父类对象,向下转型,父类对象->子类对象对于向上转型,程序会自动完成
对于向下转型时,必须明确指名要转型的类型
父类 父类对象=子类实例 ;
子类 子类对象=(子类) 父类实例;
2
当方法被覆写之后,子类调用的是被覆写之后的方法,当发生向上转型的时候,父类对象调用的是被覆写之后的方法,且转型之后的父类对象无法调用子类中特有的方法,如果
想要调用子类自己的方法,则要使用子类实例,所以可以将对象进行向下转型
3
如果想要调用子类自己的方法,则一定只能用子类声明对象,另外
在子类中调用父类中的fun2() 方法,fun2()方法要调用fun1()方法,但此时的fun1() 方法已经被子类所覆写,
所以,调用的方法是被子类覆写过的方法,在进行对象的向下转型之前,必须首先发生对象向上转型,否则将出现异常
任何的子类对象,这样无论子类如何增加,fun()方法都不用做任何的改变,因为一旦发生对象的向上转型关系后,调用的方法一定是被子类覆写过的方法
4
已知A类中有方法F1(),F2(),F3(),B类中有F1.1(),F4()且B继承了A。
(1) A a=new A(); a只能调用F1,F2,F3
(2) B b=new B(); b只能调用F1.1(),F4(),F2(),F3()
(3) A a=new B();发生了向上转型,只能调用F1.1(),F2(),F3(),
(4) A a=new B();B b=(B)a;发生了向上转型,又发生了向下转型,只能调用,F2(),F3(),F1.1(),F4()。
5
当发现可以调用一个类中的多个方法时,这里涉及方法调用的优先问题 ,优先级由高到低依次为:this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)
(super)O=(super)B=A
6
A a=new B();A确定大概可以用的方法,B确定可以用的方法(所覆盖的)
7
当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法,但是它仍然要根据继承链中方法调用的优先级来确认方法,该优先级为:this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)。