黑马程序员技术交流社区

标题: 一段程序的运行结果问题~ [打印本页]

作者: 万正    时间: 2012-3-25 12:57
标题: 一段程序的运行结果问题~
  1. class Point {
  2.         protected final int x, y;
  3.        
  4.         private final String name; // Cached at construction time
  5.        
  6.         Point(int x, int y) {
  7.                 this.x = x;
  8.                 this.y = y;
  9.                 name = makeName();
  10.         }
  11.        
  12.         protected String makeName() {
  13.                 return "[" + x + "," + y + "]";
  14.         }
  15.         public final String toString() {
  16.                 return name;
  17.         }
  18.         }
  19. public class ColorPoint extends Point {
  20.         private final String color;
  21.         ColorPoint(int x, int y, String color) {
  22.                 super(x, y);
  23.                 this.color = color;
  24.         }
  25.         protected String makeName() {
  26.                 return super.makeName() + ":" + color;
  27.         }
  28.         public static void main(String[] args) {
  29.                 System.out.println(new ColorPoint(4, 2, "purple"));
  30.         }
  31. }
复制代码
上面这段程序main 方法创建并打印了一个ColorPoint 实例。
println 方法调用了该ColorPoint 实例的toString 方法,这个方法是在Point 中定义的;
toString方法将直接返回name 域的值,这个值是通过调用makeName 方法在Point 的构造器中被初始化的;
makeName 方法被覆写为返回[x,y]:color 形式的字符串。

程序运行结果是[4,2]:null,为什么不是[4,2]:purple?
作者: 刘蕴学    时间: 2012-3-25 13:14
标题:
本帖最后由 了无尘 于 2012-3-25 13:38 编辑

这个问题是酱紫的,你的程序先运行colorpoint的构造函数,然后跳到point的构造函数,这时point里的makeName方法调用的是子类的,所以这时子类还没构造完成,就是还没执行到this.color = color这里,所以你最后得到就是null而不是purple

这也算是一种多态的表现

敢问楼主是不是故意写成酱紫的。。。怎么改都束手束脚的。。。

System.out.println(new ColorPoint(4, 2, "purple").makeName());

酱紫就好了,下边这个是运行顺序
  1. class Point
  2. {
  3.        
  4.         protected final int                x, y;
  5.        
  6.         private final String        name;        // Cached at construction time
  7.                                                                        
  8.         Point(int x, int y)
  9.         {
  10.        
  11.                 this.x = x;
  12.                 this.y = y;
  13.                 name = makeName();
  14.                 System.out.println("3");
  15.         }
  16.        
  17.         protected String makeName()
  18.         {
  19.                 System.out.println("2");
  20.                 return "[" + x + "," + y + "]";
  21.         }
  22.        
  23.         public final String toString()
  24.         {
  25.        
  26.                 System.out.println("5");
  27.                 return name;
  28.         }
  29. }

  30. public class ColorPoint extends Point
  31. {
  32.        
  33.         private String        color;
  34.        
  35.         ColorPoint(int x, int y, String color)
  36.         {
  37.                
  38.                 super(x, y);
  39.                 this.color = color;
  40.                 System.out.println("4");
  41.         }
  42.        
  43.         protected String makeName()
  44.         {
  45.        
  46.                 System.out.println("1");
  47.                 return super.makeName() + ":" + color;
  48.         }
  49.        
  50.         public static void main(String[] args)
  51.         {
  52.        
  53.                 System.out.println(new ColorPoint(4, 2, "purple"));
  54.         }
  55. }
复制代码
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. [4,2]:null
复制代码

作者: H07000223    时间: 2012-3-25 13:54
本帖最后由 H07000223 于 2012-3-25 14:04 编辑

原因就是:
super(x, y);//这一步地时候color值还没传进去,值为空,却要调用makeName()方法
this.color = color;//此时才传进去color的值

修改了下不知道是不是你想要的,看着玩吧
  1. package test;

  2. class Point
  3. {
  4.         protected final int x, y;

  5.         Point(int x, int y)
  6.         {
  7.                 this.x = x;
  8.                 this.y = y;
  9.         }
  10.        
  11.         protected String makeName()
  12.         {
  13.                 return "[" + x + "," + y + "]";
  14.         }

  15.         public final String toString()
  16.         {
  17.                 return this.makeName();
  18.         }
  19. }

  20. public class ColorPoint extends Point
  21. {
  22.         private final String color;

  23.         ColorPoint(int x, int y, String color)
  24.         {
  25.                 super(x, y);
  26.                 this.color = color;
  27.         }

  28.         protected String makeName()
  29.         {
  30.                 return super.makeName() + ":" + color;
  31.         }

  32.         public static void main(String[] args)
  33.         {
  34.                 System.out.println(new ColorPoint(4, 2, "purple"));
  35.         }
  36. }
复制代码

作者: 刘蕴学    时间: 2012-3-25 14:35
H07000223 发表于 2012-3-25 13:54
原因就是:
super(x, y);//这一步地时候color值还没传进去,值为空,却要调用makeName()方法
this.color =  ...


他那个name的声明。。。还有那个toString的声明。。。还有。。。如果都不改的话,保持原样想输出正确的,我没啥思路。。。太别扭了,就想到new完重新调用。。。
作者: H07000223    时间: 2012-3-25 14:54
了无尘 发表于 2012-3-25 14:35
他那个name的声明。。。还有那个toString的声明。。。还有。。。如果都不改的话,保持原样想输出正确的, ...

我是改不了,那些声明纯属多余,还全是final,改的蛋疼。。。
作者: 刘蕴学    时间: 2012-3-25 14:58
H07000223 发表于 2012-3-25 14:54
我是改不了,那些声明纯属多余,还全是final,改的蛋疼。。。

哈哈,那你都不如就跟我那个直接new完调用好了,删代码也多余么,呵呵,我实际上以为他故意这么写的。。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2