2014-03-06 86 views
1

我已經創建了類GConveyor的2個對象。我想在選擇它們時獲取有關這些對象的一些數據,但都返回相同的信息。謝謝您的幫助!爲什麼兩個Java對象返回相同的數據

/* 
* File: DragObjects.java 
*/ 
import acm.graphics.*; 
import acm.program.*; 
import java.awt.event.*; 

public class DragObjects extends GraphicsProgram { 
private static final long serialVersionUID = 1L; 
// Initializes the program 
public void init() { 
    //adding a few test objects 
    GConveyor conv = new GConveyor(50, 44, 120); 
    add(conv); 
    GConveyor conv2 = new GConveyor(30, 24, 60); 
    add(conv2); 

    addMouseListeners(); 
    addKeyListeners(); 
    } 

// Called on mouse press to record the coordinates of the click */ 
public void mousePressed(MouseEvent e) { 
    // GPoint has X and Y coordinate 
    last = new GPoint(e.getPoint()); 
    gobj = getElementAt(last); 
    //looking at stuff so to understand 
    getObjectData(); 
} 


public void getObjectData(){ 
    //looking at stuff so to understand 
    System.out.println(gobj.toString());  

    GContainer obLength = gobj.getParent(); 
    System.out.println(obLength.toString());  

    int d = ((GCompound) gobj).getElementCount(); 
    System.out.println("Element per GConveyor:"+ d ); 

    System.out.println("hashcode:" +gobj.hashCode()); 

    int y = this.getElementCount(); 
    System.out.println("this Element Count:"+ y ); 

    int a = GConveyor.getBF();  
    System.out.println("bf:" + a ); 
    int b = GConveyor.getEF();  
    System.out.println("ef:" + b ); 
    int h = GConveyor.getLength();  
    System.out.println("length:" + h ); 
    int c = ((GConveyor) gobj).getLength();  
    System.out.println("length:" + c );  
} 



// Called on mouse drag to reposition the object 
public void mouseDragged(MouseEvent e) { 
    if (gobj != null) { 
     gobj.move(e.getX() - last.getX(), e.getY() - last.getY()); 
     last = new GPoint(e.getPoint()); 
     } } 

/* Private instance variables */ 
private GObject gobj; 
/* The object being dragged */ 
private GPoint last; 

} 

,以下是我GConveyor類

/* 
* 
* File: GConveyor.java * This class implements a conveyor as a GCompound. 
*/ 

import java.awt.Color; 
import acm.graphics.*; 

public class GConveyor extends GCompound { 
/* Constants specifying frame size*/ 
private static final int FRAME_WIDTH = 2; 
private static int myBf; 
private static int myEf; 
private static int myLength; 

private static final double scale = 10; 
/* Private instance variables */ 
private GRect outer; 
private GRect chain_box; 
private GRect effWidth; 
private GPolygon pti; 
private GPolygon pto; 

public GConveyor(int bf, int ef, int length) { 
outer = new GRect(length, ((FRAME_WIDTH *2) + bf)); 
chain_box = new GRect(length , bf - ef); 
effWidth = new GRect(length, bf); 
pti = createAnchor(scale); 
pto = createAnchor(scale); 
add(outer, 0 , 0); 
add(chain_box, 0 , FRAME_WIDTH); 
add(effWidth, 0 , FRAME_WIDTH); 
add(pti, 0 , FRAME_WIDTH + (bf-ef) + (ef/2)); 
add(pto, length , FRAME_WIDTH + (bf-ef) + (ef/2)); 

myBf = bf; 
myEf = ef; 
myLength = length; 

} 

public static int getBF(){ 
return myBf; 
} 
public static int getEF(){ 
return myEf; 
} 
public static int getLength(){ 
return myLength; 
} 


/* Creates a hex for the Anchor */ 
private GPolygon createAnchor(double scale) { 
GPolygon poly = new GPolygon(); 
poly.addVertex(-0.25*scale, 0.433*scale); 
poly.addVertex(-0.5*scale, 0.0*scale); 
poly.addVertex(-0.25*scale, -0.433*scale); 
poly.addVertex(0.25*scale, -0.433*scale); 
poly.addVertex(0.5*scale, 0.0*scale); 
poly.addVertex(0.25*scale, 0.433*scale); 
poly.setFilled(true); 
poly.setFillColor(Color.BLUE); 
return poly; 
} 

public String toString(){ 
return("" +myBf+ +myEf+ +myLength+ ""); 

} 

} 
+0

GConveyor不會是靜態類嗎? –

+0

我有一些靜態方法 – user3386112

回答

2

這些變量是靜態的,所以這些單個變量(和它們的奇異值)將用於您的GConveyor類的所有實例。這就是爲什麼你得到相同的信息。

private static final int FRAME_WIDTH = 2; 
private static int myBf; 
private static int myEf; 
private static int myLength; 

消除static關鍵字,你不需要他們(其中每個實例需要唯一的信息),並且你應該不錯。

+1

對於'FRAME_WIDTH','static'可能是可以的。但不是其他人。 – ajb

+0

是的。真正。將'FRAME_WIDTH'留爲靜態。 – aliteralmind

1

toString()吐出myBfmyEfmyLength,所有這些都是靜態的。這意味着它們是類本身的屬性(of GConveyer),而不是類的實例(convconv2),所以當您爲其中的任何一個調用實例的setter方法時,您正在修改相同的變量。

你有2個實例,但只有1級,所以你只有1套的變量,而不是2

不要讓他們靜若你希望他們歸因於每個實例,而不是整個班級。

2

myBF,myEFmyLengthGConveyor中被聲明爲static。這意味着這些整數中只有一個是整數,並且每個整數都由全部共享。當您調用GConveyor構造函數創建第二個實例時,構造函數將設置這些靜態字段,這些靜態字段將影響爲第一個實例返回的值。

相關問題