在Java中数值不保持设定状态。

huangapple go评论76阅读模式
英文:

Values not staying set in java

问题

我有两个类,一个是snake类,另一个是block类。我正在编写一个贪吃蛇游戏。每个block代表蛇的一个部分。snake类表示整个生物体,实际上只是一个block数组(我称之为body)。block类的构造函数允许我设置x、y坐标以及在数组中的位置。然而,当我为body中的这3个block设置这些值时,它显示数组中的所有block都具有第三个block的值。(System.out.print的结果为100,100,100,而不是140,120,100)

body[0] = new block(140, 320, 0);
body[1] = new block(120, 320, 1);
body[2] = new block(100, 320, 2);
System.out.println(body[0].rect.x + "," + body[1].rect.x + "," + body[2].rect.x);

这是我的block类。

public class block {
    public static Rectangle rect;
    private static int size = 20;
    public static int position;

    public block(int x, int y, int locator) {
        rect = new Rectangle(x, y, size, size);
        position = locator;
    }
}
英文:

I have two classes, a snake class, and a block class. I am coding a snake game. The block is each piece of the snake. A snake is the whole creature, which is really just an array of blocks(which I called body). The constructor of block allows me to set the x, the y, and the position in the array of it. However, when I set these values for the 3 blocks in the body, it just says all the blocks in the array have the values of the 3rd block. (the System.out.print results in 100,100,100 instead of 140,120,100)

body[0] = new block( 140, 320, 0);
	body[1] = new block(120,320,1);
	body[2] = new block(100,320,2);
	System.out.println(body[0].rect.x+","+body[1].rect.x+","+body[2].rect.x);

Here is my block class.

public class block {
public static Rectangle rect; 
private static int size = 20;
public static int position; 
public block(int x, int y, int locator){
	rect = new Rectangle(x,y,size,size);
	position = locator;
}
}

答案1

得分: 2

你的block类中使用了静态属性。静态属性在类的所有实例之间是相同的。你应该使用非静态属性。

public class Block {
    public Rectangle rect;
    private int size = 20;
    public int position;
    
    public Block(int x, int y, int locator) {
        rect = new Rectangle(x, y, size, size);
        position = locator;
    }
}

size属性可以保持为静态,因为你希望所有的块都具有相同的大小。

一些建议:

  • 根据Java的约定,你的block类应该命名为Block
  • block类中的属性应该设置为私有,并通过getter和setter方法进行访问。
英文:

You are using static attributes in your block class. Static attributes are the same across all instances of a class. You should be using non-static attributes.

public class block {
    public Rectangle rect; 
    private static int size = 20;
    public int position; 
    public block(int x, int y, int locator){
        rect = new Rectangle(x,y,size,size);
        position = locator;
    }
}

The size attribute is fine as static since you want all the blocks to be the same size anyway.

Some additional advice:

  • your block class should be named as Block as per Java convention.
  • the attributes in your block class should be set to private and be accessed through getter and setter methods.

答案2

得分: 0

你的问题是因为你设置了块(block)类的方式。因为你将 rectposition 变量设为了公开(public),它们不是对象的实例。当你使用构造函数时,它会设置公开变量 rect 的值,而不是创建块(block)类的实例变量 rect

英文:

Your problem is because of how you've set up your block class. Because you've made the rect and position variables public, they are not instances of the object. When you use your constructor, it is setting the value of the public variable rect, instead of creating an instance variable rect of the block class.

huangapple
  • 本文由 发表于 2020年7月24日 08:41:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63065146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定