这是我的类属性,其中设置和获取变量已完成
class Attributes{
private int heroHp, heroDamage, heroArmor, currentHp, maxHp, rng,
playerAction;
private String heroName;
private boolean isAlive, run;
public void setName(String name){
heroName = name;
}
public String getName(){
return heroName;
}
public void setHp(int hp){
heroHp = hp;
maxHp = heroHp;
}
public int getHp(){
return heroHp;
}
public void setDamage(int damage){
heroDamage = damage;
}
public int getDamage(){
return heroDamage;
}
public void setArmor(int armor){
heroArmor = armor;
}
public int getArmor(){
return heroArmor;
}
public void setIsAlive(boolean isAlive){
this.isAlive = isAlive;
}
public boolean getIsAlive(){
return isAlive;
}
public void displayAttributes(){
System.out.println("====================================");
System.out.println("Hero name: " + heroName);
System.out.println("Health: " + heroHp);
System.out.println("Damage: " + heroDamage);
System.out.println("Armor: " + heroArmor);
System.out.println("====================================\n");
}
这是我的英雄班
class Heroes{
public static void main(String[] args){
Attributes slardar = new Attributes();
Attributes phantomAssassin = new Attributes();
slardar.setName("Slardar");
slardar.setHp(115);
slardar.setDamage(14);
slardar.setArmor(6);
slardar.setIsAlive(true);
phantomAssassin.setName("Phantom Assasin");
phantomAssassin.setHp(90);
phantomAssassin.setDamage(17);
phantomAssassin.setArmor(8);
phantomAssassin.setIsAlive(true);
}
}
如何在主类中调用或使用那些创建的对象?我试过了,但我真的不知道。我也尝试这样做
public class Main{
public static void main(String[] args){
Attributes attribs = new Attributes();
Heroes heroes = new Heroes();
heroes.slardar.displayAttributes();
}
}
最佳答案
您有2种主要方法,请尝试仅使用一种。按照您的Main class heros.slardar()。displayAttributes();说slardar()是Hero类中的静态方法,那里不存在。
相反,您可以这样编写代码:
class Heroes {
public static Attributes getSlardar() {
Attributes slardar = new Attributes();
slardar.setName("Slardar");
slardar.setHp(115);
slardar.setDamage(14);
slardar.setArmor(6);
slardar.setIsAlive(true);
return slardar;
}
}
public class Main {
public static void main(String[] args) {
Attributes attribs = new Attributes();
Heroes heroes = new Heroes();
heroes.getSlardar().displayAttributes();
}
}
关于java - 我有一个属性类,可以在其中设置并获取英雄属性,而英雄类则可以在其中设置英雄信息,并且希望游戏在我的主界面中进行处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55085886/