1. class Point
2. {
3. protected int x,y;
4. public Point(int a,int b)
5. {
6. x=a;
7. y=b;
8. System.out.println("Point constructor: "+this.f());//到这不理解
9.
10. }
11. public String f()
12. {
13. return "["+x+","+y+"]";
14. }
15. }
16. class Circle extends Point
17. {
18. protected double radius;
19. public Circle()
20. {
21. super(4,5);
22. radius=10;
23. System.out.println("Circle constructor: "+this.f());
24. }
25. public String f()
26. {
27. return "Center= "+super.f()+"Radius="+radius;
28. }
29. } 以上程序结果为何是:
Point constructor:Center=[4,5]Radius=0.0
Circle constructor:Center=[4,5]Radius=10.0而不是:
Point constructor:[4,5]
Circle constructor:Center=[4,5]Radius=10.0
呢????
程序执行到第8行时为何不是接着执行第11行的f()函数而是直接跳到第25行的f()函数呢?
上面第8行不是有this.f()限制是本类中的第11行的f()方法吗?为何却执行子类第25行的f()方法?多态性也不至于这样的执行吧?不解,请大虾帮忙!谢
2. {
3. protected int x,y;
4. public Point(int a,int b)
5. {
6. x=a;
7. y=b;
8. System.out.println("Point constructor: "+this.f());//到这不理解
9.
10. }
11. public String f()
12. {
13. return "["+x+","+y+"]";
14. }
15. }
16. class Circle extends Point
17. {
18. protected double radius;
19. public Circle()
20. {
21. super(4,5);
22. radius=10;
23. System.out.println("Circle constructor: "+this.f());
24. }
25. public String f()
26. {
27. return "Center= "+super.f()+"Radius="+radius;
28. }
29. } 以上程序结果为何是:
Point constructor:Center=[4,5]Radius=0.0
Circle constructor:Center=[4,5]Radius=10.0而不是:
Point constructor:[4,5]
Circle constructor:Center=[4,5]Radius=10.0
呢????
程序执行到第8行时为何不是接着执行第11行的f()函数而是直接跳到第25行的f()函数呢?
上面第8行不是有this.f()限制是本类中的第11行的f()方法吗?为何却执行子类第25行的f()方法?多态性也不至于这样的执行吧?不解,请大虾帮忙!谢
解决方案 »
- 怎么配置JAVA环境变量
- 自学JAVA?
- JTable初始化时,调用setCellEditor(editor)失败?
- 要打包发布java程序了,遇到了问题
- xp系统中,使用java如何断开adsl,如何连接adsl??
- 有关JAVA生成文本文件
- 高分求java Applet的图像处理软件代码
- 如何把 int, short 还有 String 类型的数据放到一个byte数组中?
- ================JDBC事务处理简单问题=================
- 初学者问题,为什么能编译不能执行??在线等待??
- 小弟在写个JAVA的记事本,请问如何在文本区实现自动换行???
- 关于如何在STACK中增加数组元素
2. {
3. protected int x,y;
4. public Point(int a,int b)
5. {
6. x=a;
7. y=b;
8. System.out.println("Point constructor: "+this.f());//到这不理解
9.
10. }
11. public String f()
12. {
13. return "["+x+","+y+"]";
14. }
15. }
16. class Circle extends Point
17. {
18. protected double radius;
19. public Circle()
20. {
21. super(4,5);
22. radius=10;
23. System.out.println("Circle constructor: "+this.f());
24. }
25. public String f()
26. {
27. return "Center= "+super.f()+"Radius="+radius;
28. }
29. }
30. public class Test1
31. {
32. public static void main(String[] args)
33. {
34. Circle c=new Circle();
35. }
36. };以上程序结果为何是:
Point constructor:Center=[4,5]Radius=0.0
Circle constructor:Center=[4,5]Radius=10.0而不是:
Point constructor:[4,5]
Circle constructor:Center=[4,5]Radius=10.0
呢????
程序执行到第8行时为何不是接着执行第11行的f()函数而是直接跳到第25行的f()函数呢?
上面第8行不是有this.f()限制是本类中的第11行的f()方法吗?为何却执行子类第25行的f()方法?多态性也不至于这样的执行吧?不解,请大虾帮忙!谢