今天看马士兵的java视频,在内存分析这里,有了个问题
先把代码贴上来
视频中讲解main程序中,下面这一行时说,先从new Circle(new Point(1.0, 2.0), 2.0) 括号里面的内容开始分析,也就是说先分析new Point(1.0, 2.0)
Circle c1 = new Circle(new Point(1.0, 2.0), 2.0);
但我很纳闷,到底new Circle() 和 new Point()到底哪一个先执行,幸好例子程序中有相应的代码,然后加上一句System.out.println(),就可以知道哪一个constructor先工作了
重点看代码中的红色部分
全部代码如下
1 classPoint {2 private doublex;3 private doubley;4
5 Point(double x1, double y1) {
6 System.out.println("Point, Point");7 x =x1;8 y =y1;9 }10
11 public doublegetX() {12 returnx;13 }14
15 public doublegetY() {16 returny;17 }18
19 public void setX(doublei) {20 x =i;21 }22
23 public void setY(doublei) {24 y =i;25 }26 }27
28 classCircle {29 privatePoint o;30 private doubleradius;31
32 Circle(Point p, double r) {
33 System.out.println("Circle, Circle");
34 o = p;
35 radius = r;36 }37
38 Circle(double r) {
39 System.out.println("Circle, Circle");40 o = new Point(0.0, 0.0);41 radius =r;42 }43
44 booleancontains(Point p) {45 double x = p.getX() -o.getX();46 double y = p.getY() -o.getY();47 double value = x * x + y *y;48 if (value > radius *radius ) {49 return false;50 }51 return true;52 }53
54 public void setO(double x, doubley) {55 o.setX(x);56 o.setY(y);57 }58
59 publicPoint getO() {60 returno;61 }62
63 public doublegetRadius() {64 returnradius;65 }66
67 public void setRadius(doubler) {68 radius =r;69 }70
71 public doublearea() {72 return 3.14 * radius *radius;73 }74 }75
76 public classTestCircle {77 public static voidmain(String [] args) {78 Circle c1 = new Circle(new Point(1.0, 2.0), 2.0);
79 System.out.println("*********************************************************");
80 Circle c2 = new Circle(5.0);81 System.out.println("c1: (" + c1.getO().getX() + "," + c1.getO().getY() + "), "+c1.getRadius());82 System.out.println("c2: (" + c2.getO().getX() + "," + c2.getO().getY() + "), "+c2.getRadius());83 System.out.println("c1 area = " +c1.area());84 System.out.println("c2 area = " +c2.area());85
86 Point p1 = new Point(5.2, 6.3);87 System.out.println(c1.contains(p1));88 System.out.println(c1.contains(new Point(10.0, 9.0)));89 }90 }
结果运行如下图
所以对于
Circle c1 = new Circle(new Point(1.0, 2.0), 2.0);
先运行的是new Point(), 也就是Point的constructor程序先运行,
然后才是new Circle
而对于下面的
Circle c2 = new Circle(5.0);
则是new Circle() 先运行,然后在其constructor内部,再调用的new Point()
here is 分界线————————————————————————————————————————————————————————————
20:08:37
进行一下内容添加
再次附上全部代码,跟上面的几乎一样,改了一点点
1 classPoint {2 private doublex;3 private doubley;4
5 Point(double x1, double y1) {
6 System.out.println("Point, Point");
7 x = x1;
8 y = y1;9 }10
11 public doublegetX() {12 returnx;13 }14
15 public doublegetY() {16 returny;17 }18
19 public void setX(doublei) {20 x =i;21 }22
23 public void setY(doublei) {24 y =i;25 }26 }27
28 classCircle {29 privatePoint o;30 private doubleradius;31
32 Circle(Point p, doubler) {33 System.out.println("Circle, Circle");34 o =p;35 radius =r;36 }37
38 Circle(doubler) {39 System.out.println("Circle, Circle");40 o = new Point(0.0, 0.0);41 radius =r;42 }43
44 boolean contains(Point p) {
45 System.out.println("here is the contains method");46 double x = p.getX() -o.getX();47 double y = p.getY() -o.getY();48 double value = x * x + y *y;49 if (value > radius *radius ) {50 return false;51 }52 return true;53 }54
55 public void setO(double x, doubley) {56 o.setX(x);57 o.setY(y);58 }59
60 publicPoint getO() {61 returno;62 }63
64 public doublegetRadius() {65 returnradius;66 }67
68 public void setRadius(doubler) {69 radius =r;70 }71
72 public doublearea() {73 return 3.14 * radius *radius;74 }75 }76
77 public classTestCircle {78 public static voidmain(String [] args) {79 Circle c1 = new Circle(new Point(1.0, 2.0), 2.0);80 System.out.println("*********************************************************");81 Circle c2 = new Circle(5.0);82
83 System.out.println("------------------------------------------");84
85 System.out.println("c1: (" + c1.getO().getX() + "," + c1.getO().getY() + "), "+c1.getRadius());86 System.out.println("c2: (" + c2.getO().getX() + "," + c2.getO().getY() + "), "+c2.getRadius());87 System.out.println("c1 area = " +c1.area());88 System.out.println("c2 area = " +c2.area());89
90 System.out.println("_______________________________________________");91
92 c1.setO(5, 6);93 c2.setRadius(9.0);94 System.out.println("c1: (" + c1.getO().getX() + "," + c1.getO().getY() + "), "+c1.getRadius());95 System.out.println("c2: (" + c2.getO().getX() + "," + c2.getO().getY() + "), "+c2.getRadius());96 System.out.println("c1 area = " +c1.area());97 System.out.println("c2 area = " +c2.area());98
99 System.out.println("_______________________________________________");100
101
102 Point p1 = new Point(5.2, 6.3);103 System.out.println(c1.contains(p1));104 System.out.println(c1.contains(new Point(10.0, 9.0)));105 }106 }
对于上面代码的104行,结果再次证明,先new Point(), 再 c1.contains()