gpt4 book ai didi

java - 向类中添加一个简单的 GIF

转载 作者:行者123 更新时间:2023-12-01 14:10:08 25 4
gpt4 key购买 nike

我正在上编程 2 课,教授希望我们制作一门最终会发展成游戏的类(class)。我遇到的问题是我不知道如何将文件添加到类中,以便当我使用测试器时不会出现错误。目前我还不必对该文件执行任何操作。我只需要在 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);




}

}

最佳答案

您可以使用文件中的简单方法

        File file = new File("folder/folder/folder/file.gif");
//absolute path
file.getAbsolutePath();
//path
file.getPath();

在您的测试类上使用它并将对象放入 myPlayer2 构造函数中。

在 toString() 方法中,在变量中

sprite

使用

sprite.getPath() or sprite.getAbsolutePath();

用于以字符串格式显示文件路径。

如果你想使用流来读取字节,试试这个

InputStream in = new FileInputStream(new File("folder/folder/folder/file.gif"));

关于java - 向类中添加一个简单的 GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18579349/

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