一)相关类
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)); ⑨
(三)答案
① A and A
② A and A
③ A and D
④ B and A
⑤ B and A
⑥ A and D
⑦ B and B
⑧ B and B
⑨ A and D
转摘http://blog.youkuaiyun.com/thinkGhoster/article/details/2307001
本文详细解析了面向对象编程中的继承与多态概念,通过具体代码实例展示了如何实现类的继承和方法的多态性。重点讨论了不同类之间的关系,以及如何利用多态提高代码的复用性和灵活性。
1415

被折叠的 条评论
为什么被折叠?



