我在我的编程 2 课上,教授希望我们做一门最终会发展成游戏的课。我遇到的问题是我不知道如何将文件添加到类中,以便在使用测试仪时不会出错。我还不需要对文件做任何事情。我只需要在 myPlayer2 之后在测试器中创建几个实例,但我不知道该怎么做。我希望这和我的代码有意义。谢谢你。
import java.io.File;
public class myPlayer {
private String name;
private String gender;
private String role;
private int health;
private String state;
private File sprite = new File("MarioSprite.gif");
/**
* constructor for the game player
* @param name-- the players name
* @param gender -- the players gender
* @param role -- role such as boss, healer, demon, knight
* @param health -- percentage of health
* @param state -- whether player is alive, dead, active, inactive, sleeping
* @param sprite -- this is the sprite file for the class
*/
// the file name is MarioSprite.gif
public myPlayer(){
name = " unknown";
gender = " unknown";
role = " not defined";
health = 0;
state = " not defined";
sprite = null;
}
public myPlayer(String name, String gender, String role, int health, String state, File sprite){
this.name = name;
this.gender = gender;
this.role = role;
this.health = health;
this.state = state;
this.sprite = sprite;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* @return the role
*/
public String getRole() {
return role;
}
/**
* @param the role to set
*/
public void setRole(String role) {
this.role = role;
}
/**
* @return the health
*/
public int getHealth() {
return health;
}
/**
* @param the health to set
*/
public void setHealth(int health) {
this.health = health;
}
/**
* @return the state
*/
public String getState() {
return state;
}
/**
* @param the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* @return the sprite
*/
public File getSprite() {
return sprite;
}
/**
* @param the sprite to set
*/
public void setSprite(File sprite) {
this.sprite = sprite;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "myPlayer [name=" + name + ", gender=" + gender + ", role="
+ role + ", health=" + health + ", state=" + state
+ ", sprite=" + sprite + "]";
}
}
public class myPlayerTester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
myPlayer myPlayer1, myPlayer2, myPlayer3, myPlayer4;
myPlayer1 = new myPlayer();
System.out.println(myPlayer1);
myPlayer2 = new myPlayer("SteveO", "Male", "demon", 99, "live", null );
System.out.println(myPlayer2);
myPlayer3 = new myPlayer("Gandolf", "Female", "demon", 93, "live", null);
System.out.println(myPlayer3);
myPlayer4 = new myPlayer("Roger", "Female", "demon", 69, "inactive", null);
System.out.println(myPlayer4);
}
}