這個例子有三個類:Point,Rectangle和CreateObjectDemo。運行程序後,rectTwo的原點爲23,94,但根據構造函數「public Rectangle(int w,int h)」,我認爲它應該是0,0。我正在使用這個Oracle在線java類,不明白origen點是如何(23,94)而不是0,0
我在想這條線「Rectangle rectTwo = new Rectangle(50,100);」將調用「public Rectangle(int w,int h)」構造函數,所以原點將爲0,0,因爲構造函數的第一行是「origin = new Point(0,0);」 但是,當我打印的原產地價值,我得到23,94。有人能幫我理解爲什麼起點是23,94?任何幫助將不勝感激。
public class Point {
public int x = 0;
public int y = 0;
public Point(int a, int b) {
x = a;
y = b;
}
}
public class Rectangle {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
}
public class CreateObjectDemo {
public static void main(String[] args) {
// Declare and create a point object and two rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
// display rectOne's width, height, and area
System.out.println("Origen of rectone: " + rectOne.origin.x);
System.out.println("Origen of rectone: " + rectOne.origin.y);
// set rectTwo's position
rectTwo.origin = originOne;
// display rectTwo's position
System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
}
}
你覺得'rectTwo.origin = originOne'呢? –