B
Bruce Sam
/* *************************************************** */
class Point { // A messenger
public int x, y, z; // Since it's just a carrier
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public Point(Point p) { // Copy-constructor
this.x = p.x;
this.y = p.y;
this.z = p.z;
}
public String toString() {
return "x: " + x + " y: " + y + " z: " + z;
}
}
class Space {
public static Point translate(Point p) {
// p = new Point(p); //Notice!
p.x = p.x + 1;
p.y = p.y + 1;
p.z = p.z + 1;
return p;
}
}
public class MessengerDemo {
public void test() {
Point p1 = new Point(1, 2, 3);
Point p2 = Space.translate(p1);
String result = "p1: " + p1 + " p2: " + p2;
System.out.println(result);
}
public static void main(String[] args) {
MessengerDemo demo = new MessengerDemo();
demo.test();
}
}
/* *************************************************** */
The above is my simply test program.No matter I remove the line
"p = new Point(p);",the output result is the same:
".p1: x: 1 y: 2 z: 3 p2: x: 2 y: 3 z: 4",But in my understanding,if i
remove the line "p = new Point(p)",the output result should be ".p1: x:
2 y: 3 z: 4 p2: x: 2 y: 3 z: 4".Is it right?
class Point { // A messenger
public int x, y, z; // Since it's just a carrier
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public Point(Point p) { // Copy-constructor
this.x = p.x;
this.y = p.y;
this.z = p.z;
}
public String toString() {
return "x: " + x + " y: " + y + " z: " + z;
}
}
class Space {
public static Point translate(Point p) {
// p = new Point(p); //Notice!
p.x = p.x + 1;
p.y = p.y + 1;
p.z = p.z + 1;
return p;
}
}
public class MessengerDemo {
public void test() {
Point p1 = new Point(1, 2, 3);
Point p2 = Space.translate(p1);
String result = "p1: " + p1 + " p2: " + p2;
System.out.println(result);
}
public static void main(String[] args) {
MessengerDemo demo = new MessengerDemo();
demo.test();
}
}
/* *************************************************** */
The above is my simply test program.No matter I remove the line
"p = new Point(p);",the output result is the same:
".p1: x: 1 y: 2 z: 3 p2: x: 2 y: 3 z: 4",But in my understanding,if i
remove the line "p = new Point(p)",the output result should be ".p1: x:
2 y: 3 z: 4 p2: x: 2 y: 3 z: 4".Is it right?