通过创建新对象来更改字段

时间:2014-06-06 14:17:41

标签: java inheritance

当我创建船时,其字段:height = 50和width = 29,如我所愿。但是在我创建阻止后,它将船舶的字段(高度和宽度)更改为25.你能告诉我为什么吗?

代码示例:

班级单位:

package test;


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Unit {
    private int x,y;
    private String imageFileName;
    private BufferedImage img;

    private int a,b,c,d;

    private static int height;

    private static int width;

    public Unit(String imageFileName, int width, int height, int x, int y) {
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
        a=y;
        b=x+width;
        c=y+height;
        d=x;
    }

    public int getX() {
        return x;
    }
    int getY() {
        return y;
    }
    public void setY(int i) {
        y=i;
        setEdges();
    }
    public void setX(int i) {
        x=i;
        setEdges();
    }
    public void setEdges() {
        a=y;
        b=x+width;
        c=y+height;
        d=x;
    }
    public void showUnit() {
        System.out.println("x = " + x + " y= " + y);
        System.out.println("a = " + a + " b= " + b);
        System.out.println("c = " + c + " d= " + d);
        System.out.println("height = " + height + " width= " + width);
    }

}

类阻止:

package test;


import java.awt.Image;

public class Block extends Unit {
    public static int BLOCK_SIZE=25;
    public Block(int number, int x, int y) {    
        super("kafelki/"+Integer.toString(number)+".png", BLOCK_SIZE, BLOCK_SIZE, x, y);
    }   
    public int getX() {
        return super.getX();
    }

    public int getY() {
        return super.getY();
    }
    public void show() {
        super.showUnit();
    }
}

班级船舶:

package test;


import static java.lang.Math.abs;

import java.awt.Image;

public class Ship extends Unit {
    public static int HEIGHT_C=50;
    public static int WIDTH_C=29;
    private double vUpDown;
    private double vLeftRight;
    public Ship(int x, int y) {
        super("1", WIDTH_C, HEIGHT_C, x, y);
    }
    public int getX() {
        return super.getX();
    }
    public int getY() {
        return super.getY();
    }
    public void setY(int i) {
        super.setY(i);
    }

    public void setX(int i) {
        super.setX(i);
    }

    public void showShip() {
        System.out.println("####SHIP####");
        super.showUnit();
        System.out.println("Vupdown = " + vUpDown + " Vleftright= " + vLeftRight);
        System.out.println("############");
    }

} 

课程测试:

public class Test {
    public static void main(String[] args) {
        Ship ship = new Ship(100,100);
        ship.showShip();
        Block block = new Block (1, 200, 200);
        ship.showShip();
    }
}

1 个答案:

答案 0 :(得分:2)

这是问题所在:

private static int height;
private static int width;

这些字段是静态的,这意味着它们是为类型本身设置的,而不是特定于该类型的任何实例

我强烈怀疑他们应该只是实例字段:

private int height;
private int width;

作为一般规则,如果您在构造函数中设置静态字段,则应该是警告标志。