2014-03-19 142 views
0

這將是相當長的囉嗦,因爲我對Java作爲一種語言還比較陌生。爲什麼初始值會改變而不是對象值?

該程序有一個叫Point的類。這會爲形狀的頂點創建點。

public class Point { 

     private int x; 
     private int y; 

    public Point(int x, int y){ 

     this.x = x; 
     this.y = y; 

    } 

在該類中,有方法,例如距離(點2之間),縮放(通過因子增加),翻譯(需要一個x和y座標,並增加了它this.x和XY)等上。

然後我有3個類來創建形狀。三角形,矩形和圓形,構造函數看起來像這樣。

public class Triangle { 

     private int sides; 
     private Point[] points; 

    public Triangle(Point[] vertices) { 

     this.sides = 3; 
     this.points = vertices; 

} 

public class Rectangle { 

     private int sides; 
     private Point topCorner; 
     private int width; 
     private int length; 



    public Rectangle(Point corner, int w, int l){ 

     this.sides = 4; 
     this.topCorner = corner; 
     this.width = w; 
     this.length = l; 

} 

public class Circle { 

     private Point center; 
     private int radius; 

    public Circle(Point center, int radius){ 

     this.center = center; 
     this.radius = radius; 

} 

所有這些類有相同的方法平移和縮放。這裏顯示了一個例子。

//For the Rectangle Class 
    public void translate(int dx, int dy){ 

     topCorner.translate(dx, dy); 

} 

我的問題是在我的主類。會發生什麼是我創建了3個點,並使用全部3作爲數組來創建三角形,其他2個用作矩形和圓的點。現在,當我一次調用所有3個對象的翻譯方法時,它最終會使值翻倍,因爲我相信我的錯誤是我已經在某處傳遞了對「p1」,「p2」和「p3」的引用,而我改變它們的值而不是對象值。下面是代碼,和後

public class GraphicsDemo { 


public static void main(String[] args) { 
    Point p1 = new Point(0,20); 
    Point p2 = new Point(20,0); 
    Point p3 = new Point(30,30); 


    Point[] vertices = {p1,p2,p3}; 

    Triangle t = new Triangle(vertices); 
    Rectangle r = new Rectangle(p1,10,15); 
    Circle c = new Circle (p2, 25); 


    System.out.println("Triangle" + "\n" + t.toString() + "\n" + "area:" + t.area() + "\n" + "perimeter: " + t.perimeter()); 
    System.out.println("Reactangle" + "\n" + r.toString() + "\n" + "area:" + r.area() + "\n" + "perimeter: " + r.perimeter()); 
    System.out.println("Circle" + "\n" + c.toString() + "\n" + "area:" + c.area() + "\n" + "perimeter: " + c.perimeter()); 

    c.translate(10, 15); 
    t.translate(10, 15); 
    r.translate(10, 15); 

System.out.println("Triangle" + "\n" + t.toString() + "\n" + "area:" + t.area() + "\n" + "perimeter: " + t.perimeter()); 
    System.out.println("Reactangle" + "\n" + r.toString() + "\n" + "area:" + r.area() + "\n" + "perimeter: " + r.perimeter()); 
    System.out.println("Circle" + "\n" + c.toString() + "\n" + "area:" + c.area() + "\n" + "perimeter: " + c.perimeter());; 

這種情況的輸出是我將說明輸出,代替添加10和15所述的形狀的座標,20和30,如果點是在一個以上的使用被添加形狀。我確信這是因爲我實際上改變了點而不是對象的價值,但我不知道如何改變它。

謝謝任何​​可以提前幫忙的人。這是我第一次嘗試了很多東西,並且遇到了困難。

回答

0

您錯過了顯示代碼最重要的部分,但它看起來像您的分析是正確的。在你的翻譯代碼中,你實際上需要生成新的點實例(並替換它們)。

這通常是一個好主意,不要修改對象,但要保持不變。確保這一點的最佳方法是跳過安裝程序:

class Point { 
    Point(int x, int y) { this.x = x; this.y = y; } 
    int getX() { return x; } intGetY() { return y; } 

    /** Create a new point displaced by given coordinates. */ 
    Point translate(int deltaX, int deltaY) 
    { 
    return new Point(this.getX() + deltaX, this.getY() + deltaY); 
    } 
} 
0

您的假設是正確的,即傳遞引用。圓形,三角形,矩形都在其構造函數中採用與其他形狀相同的一個或多個點。請參閱矩形,它採用與三角形相同的點(p1)。

Point[] vertices = {p1,p2,p3}; 

Triangle t = new Triangle(vertices); 
Rectangle r = new Rectangle(p1,10,15); 
Circle c = new Circle (p2, 25); 

一個簡單的解決方案是克隆構造函數中的點,就像我在下面的Triangle類中做的那樣。這意味着兩個形狀將共享相同的x和y,但對一個形狀的更改將不再直接影響另一個形狀。

一個類是一個對象引用。所以你需要創建一個新的對象引用來創建單獨的對象來處理。這是什麼。clone()方法我向你添加了point類,它複製了point的內部並創建了一個精確的副本。其他方法的構造函數然後在您提供它們的點上調用克隆方法,以便它們只處理您給它們的點的副本。

class Point{ 

public int x; 
public int y; 

Point(int x, int y){ 
    this.x = x; 
    this.y = y; 
} 

public Point clone(){ 

    return new Point(x,y); 

} 

}

class Triangle{ 

final static int m_sides = 3; //constant 
Point[] m_points; 

Triangle(Point[] points){ 

    //You need to clone your ponits. 
    m_points = new Point[]{ 
      points[0].clone(), 
      points[1].clone(), 
      points[2].clone()}; 

} 
0

他們的確是同一個對象。即三角形t有p1。如果你改變矩形p1,你也在改變三角形p1。

你應該在Point's中做很深的克隆,使它們獨立。

相關問題