Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么两个Java对象都返回相同的数据_Java - Fatal编程技术网

为什么两个Java对象都返回相同的数据

为什么两个Java对象都返回相同的数据,java,Java,我已经创建了2个GConveyor类的对象。我想在选择对象时获取它们的一些数据,但两者都返回相同的信息。谢谢你的帮助 /* * File: DragObjects.java */ import acm.graphics.*; import acm.program.*; import java.awt.event.*; public class DragObjects extends GraphicsProgram { private static final long seria

我已经创建了2个GConveyor类的对象。我想在选择对象时获取它们的一些数据,但两者都返回相同的信息。谢谢你的帮助

/* 
* 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+ "");

}

}
你的toString会吐出myBf myEf和myLength,它们都是静态的。这意味着它们是GConveyer类本身的属性,而不是conv和conv2类实例的属性,因此当您为其中任何一个调用实例的setter方法时,您正在修改相同的变量

您有两个实例,但只有一个类,所以您只有一组这些变量,而不是两个

如果希望它们归属于每个实例,而不是整个类,请不要将它们设置为静态。

您的toString会显示myBf myEf和myLength,它们都是静态的。这意味着它们是GConveyer类本身的属性,而不是conv和conv2类实例的属性,因此当您为其中任何一个调用实例的setter方法时,您正在修改相同的变量

您有两个实例,但只有一个类,所以您只有一组这些变量,而不是两个


如果希望将它们归属于每个实例,而不是整个类,请不要将它们设置为静态。

这些变量是静态的,因此这些单个变量及其奇异值将用于GConveyor类的所有实例。这就是为什么你会得到同样的信息

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

在每个实例中都需要唯一信息的情况下,请删除static关键字。这些变量是静态的,因此这些单个变量及其奇异值将用于GConveyor类的所有实例。这就是为什么你会得到同样的信息

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

在每个实例中都需要唯一信息的情况下,删除不需要的static关键字,这样应该很好。

myBF、myEF和myLength在GConveyor中声明为static。这意味着每个整数只有一个,并且每个整数都由类的所有对象共享。当您调用GConveyor构造函数来创建第二个实例时,构造函数会设置这些静态字段,然后影响为第一个实例返回的值

myBF、myEF和myLength在GConveyor中声明为静态。这意味着每个整数只有一个,并且每个整数都由类的所有对象共享。当您调用GConveyor构造函数来创建第二个实例时,构造函数会设置这些静态字段,然后影响为第一个实例返回的值

GConveyor不会是静态类,对吧?我有一些静态方法GConveyor不会是静态类,对吧?我有一些静态方法,静态可能适合帧宽。但其他人不一样,是的。符合事实的将“帧宽度”保留为静态。对于“帧宽度”,静态可能是可以的。但其他人不一样,是的。符合事实的保持框架宽度为静态。