Question 41
41. Given:
10. class One {
11. public One foo() { return this; }
12. }
13. class Two extends One {
14. public One foo() { return this; }
15. }
16. class Three extends Two {
17. // insert method here
18. }
Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
A. public void foo() { } //问题一:为什么A不可以?为什么B不可以?为什么E不可以?
B. public int foo() { return 3; }
C. public Two foo() { return this; }
D. public One foo() { return this; }//问题二:return this以后,原本返回的是Two,可以由于标明返回类型是One所以就又有了一个操作是把返回的Two强制转换为One,是吗?
E. public Object foo() { return this; }
Answer: CD谢谢。
41. Given:
10. class One {
11. public One foo() { return this; }
12. }
13. class Two extends One {
14. public One foo() { return this; }
15. }
16. class Three extends Two {
17. // insert method here
18. }
Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
A. public void foo() { } //问题一:为什么A不可以?为什么B不可以?为什么E不可以?
B. public int foo() { return 3; }
C. public Two foo() { return this; }
D. public One foo() { return this; }//问题二:return this以后,原本返回的是Two,可以由于标明返回类型是One所以就又有了一个操作是把返回的Two强制转换为One,是吗?
E. public Object foo() { return this; }
Answer: CD谢谢。
重写问题:Three extends Two 后继承了foo()方法,如果要重写这个方法,参数、返回类型、名称都相同。而这里的三个选项返回类型都不同。就算是重载也要参数不同。问题二:return this以后,原本返回的是Two,可以由于标明返回类型是One所以就又有了一个操作是把返回的Two强制转换为One,是吗?
不因该是强制转型,而向上转型,详细可以看《thinkjava》
可能是你写错了,红色的地方因该是返回Three,