创建全局对象数组 (java)

Creating global array of objects (java)

所以我正在做一个 Space Invaders 主题项目,我的大部分 classes 和 运行 已经开始制作动画。部分过程是船的武器。

我有一个class的武器,如下(关注构造函数):

    /**
 * @(#)Weapon.java
 *
 *
 * @author Tristan Nel - 18179460
 * @version 1.00 2015/3/4
 */


public class Weapon {

    private String type;
    private int damage;
    private int rof; //rate of fire
    private int orientation;
    private int firingStage; //0 - not firing ; 1 - flash & recoil ; 2 - bullet
    private String[] sprites; //Set of sprite image file names

    public Weapon() {
    }

    public Weapon(String type, int damage, int rof, int orientation, int firingStage, String[] sprites)
    {
        this.type = type;
        this.damage = damage;
        this.rof = rof;
        this.orientation = orientation;
        this.firingStage = firingStage;
        this.sprites = sprites;
    }

    //GET and SET Methods
    public void setType(String type)
    {
        this.type = type;
    }

    public void setDamage(int damage)
    {
        this.damage = damage;
    }

    public void setROF(int rof)
    {
        this.rof = rof;
    }

    public void setOrientation(int orientation)
    {
        this.orientation = orientation;
    }

    public void setFiringStage(int firingStage)
    {
        this.firingStage = firingStage;
    }

    public void setSprites(String[] sprites)
    {
        this.sprites = sprites;
    }

    public String getType()
    {
        return this.type;
    }

    public int getDamage()
    {
        return this.damage;
    }

    public int getROF()
    {
        return this.rof;
    }

    public int getOrientation()
    {
        return this.orientation;
    }

    public int getFiringStage()
    {
        return this.firingStage;
    }

    public String[] getSprites()
    {
        return this.sprites;
    }

}

在另一个 class 中,它处理游戏屏幕上要动画的所有元素,我想要一个全局的硬编码武器类型数组,可以根据需要轻松访问。我试图在 class:

的内容顶部这样做
    /**
 * @(#)GameScreen.java
 *
 *
 * @author Tristan Nel - 18179460
 * @version 1.00 2015/3/4
 */

import java.util.Scanner;
import java.io.*;

public class GameScreen {

    private static final String HIGH_SCORE_FILE = "highScore.txt";

    //Available Weapons
    //UPDATED SINCE ORIGINAL POST
    public static final Weapon[] WEAPONS = new Weapon[4];
    WEAPONS[0] = new Weapon("Machinegun",       10, 20, 0, 0,   {Graphics.MG_L_NORM, Graphics.MG_R_NORM});
    WEAPONS[1] = new Weapon("Plasma MG",        20, 20, 0, 0,   {Graphics.PMG_L_NORM, Graphics.PMG_R_NORM});
    WEAPONS[2] = new Weapon("Photon Cannon",        40, 5, 0, 0,    {Graphics.PC_L_NORM, Graphics.PC_R_NORM});
    WEAPONS[3] = new Weapon("Alien Destabilizer",   60, 10, 0, 0,   {Graphics.AD_L_NORM, Graphics.AD_R_NORM});

    private Ship defender;
    private Weapon equipped;
    //private Invader[] aliens;
    //private Bullet[] bullets;
    private int score;
    private int highscore;
    private int lives;

    public GameScreen() {
    }

    public GameScreen(Ship defender, int score, int lives)
    {
        this.defender = defender;
        this.score = score;
        this.lives = lives;
    }

    public void loadHighscore()
    {
        try
        {
            Scanner sc = new Scanner(new File(HIGH_SCORE_FILE));
            this.highscore = Integer.parseInt(sc.next());
            sc.close();
        }
        catch(FileNotFoundException fnf)
        {
            System.out.println(fnf);
            this.highscore = 0;
        }

    }

    public void saveHighScore(int highscore)
    {
        try
        {
            FileWriter write = new FileWriter(HIGH_SCORE_FILE);
            PrintWriter pw = new PrintWriter(write);
            pw.print(this.highscore);

            pw.close();
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }

    //GET and SET methods
    public void setDefender(Ship defender)
    {
        this.defender = defender;
    }

    public void setScore(int score)
    {
        this.score = score;
    }

    public void setLives(int lives)
    {
        this.lives = lives;
    }

    public Ship getDefender()
    {
        return this.defender;
    }

    public int getScore()
    {
        return this.score;
    }

    public int getLives()
    {
        return this.lives;
    }

}

当我尝试向数组添加另一个元素时,每行都会出现以下错误消息:

已更新 https://drive.google.com/file/d/0B7ye7Ul2JDG2NDFDRTJNM1FCd0U/view?usp=sharing

非常令人沮丧.. 我在某处读到你必须在方法中创建一个对象? (例如 main() ) 但是我在我的驱动程序中尝试了 class 并且没有任何区别...

将不胜感激 help/advice (:

实际上我之前写错了,但看起来你需要像这样使用 new 运算符调用构造函数。

arrayName[0] = new Weapon();

存在多个问题

  • 您的 class 正文中不能包含任意代码,例如WEAPONS[0] = 来电。但是,您可以使用 new Type[]{} 语法直接初始化数组。您也可以使用静态初始值设定项 static {} 但不推荐这样做。

  • 此外,您需要通过 new 关键字使用构造函数,它不仅仅是一个方法,即 new Weapon() 而不是 Weapon()

  • 您不能使用 {} 声明数组,即 new String[]{{Graphics.MG_L_NORM, Graphics.MG_R_NORM}} 而不是 {Graphics.MG_L_NORM, Graphics.MG_R_NORM}

工作版本

public static final Weapon[] WEAPONS = new Weapon[] {
    new Weapon("Machinegun",       10, 20, 0, 0,   new String []{Graphics.MG_L_NORM, Graphics.MG_R_NORM}),
    new Weapon("Plasma MG",        20, 20, 0, 0,   new String []{Graphics.PMG_L_NORM, Graphics.PMG_R_NORM}),
    new Weapon("Photon Cannon",        40, 5, 0, 0,    new String []{Graphics.PC_L_NORM, Graphics.PC_R_NORM}),
    new Weapon("Alien Destabilizer",   60, 10, 0, 0,   new String []{Graphics.AD_L_NORM, Graphics.AD_R_NORM})
};

因为那些 classes 看起来有些静态,所以要研究的其他事情是为此使用枚举。当您必须搜索特定武器时,这将有助于避免并发症。更好的设计是让 WeaponType 枚举包含与武器相关的所有静态不可变数据,并让武器 class 包含所有状态数据。