gpt4 book ai didi

java - 创建全局对象数组 (java)

转载 作者:行者123 更新时间:2023-11-30 08:55:45 24 4
gpt4 key购买 nike

所以我正在做一个太空入侵者主题项目,我的大部分类(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)内容的顶部这样做:

    /**
* @(#)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() )但是我在我的驱动程序类中尝试过,但没有任何区别......

将不胜感激任何帮助/建议(:

最佳答案

有多个问题

  • 你不能在类的主体中包含任意代码,例如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})
};

关于java - 创建全局对象数组 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28887598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com