我通过创建游戏来学习java,我有:
public abstract class Char {
private int hp;
private int def;
private int power;
private int intelligence;
privat1e int lvl;
private int xp;
public void levelUp()
{
lvl += 1;
hp *= 2;
def *= 2;
power *= 2;
intelligence *= 2;
}
public void basicalAttack(Char ennemy)
{
ennemy.hp = ennemy.hp - (power - ennemy.def);
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public int getIntelligence() {
return intelligence;
}
public void setIntelligence(int intelligence) {
this.intelligence = intelligence;
}
public int getLvl() {
return lvl;
}
public void setLvl(int lvl) {
this.lvl = lvl;
}
public int getXp() {
return xp;
}
public void setXp(int xp) {
this.xp = xp;
}
}
然后我有两类人物:
public class Wizard extends Char {
public Wizard() {
setLvl(1);
setHp(400);
setDef(150);
setPower(20);
setIntelligence(200);
}
public void fireBallAttack(Char ennemy)
{
ennemy.setHp(ennemy.getHp() - (this.getIntelligence() + 20 - ennemy.getDef()));
}
}
和
public class Warrior extends Char {
public Warrior() {
setLvl(1);
setHp(1000);
setDef(250);
setPower(100);
setIntelligence(20);
}
public void swordAttack(Char ennemy)
{
ennemy.setHp(ennemy.getHp() - (this.getPower() + 10 - ennemy.getDef()));
}
}
所以我希望我的可玩角色选择一个班级:
public class PlayableChar extends Char {
private String name;
private Char playable;
public PlayableChar(String name, String job) {
this.setName(name);
switch (job)
{
case "warrior" :
setPlayable(new Warrior());
break;
case "Wizard" :
setPlayable(new Wizard());
break;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Char getPlayable() {
return playable;
}
public void setPlayable(Char playable) {
this.playable = playable;
}
}
所以我向用户询问他想在我的主要课程中使用哪个课程
Scanner readline = new Scanner(System.in);
String jobName = "";
String Name = "";
System.out.print("Your name: ");
Name = readline.nextLine();
jobName = readline.nextLine();
Playable mainChar = new Playable(Name, jobName);
实际上它有效,但是当我想使用mainChar.getPlayable().fireBallAttack(ennemy)
的方法时,它不起作用我得到了
对于Type Char,fireBall(ennemy)未定义;
我是否需要更改Warrior和Wizard类?或者是其他东西 ?感谢。
答案 0 :(得分:1)
根据您的回复和代码,我假设问题中存在s few typo
和错误。在提供解决方案之前,我想指出这些。
a)
Playable mainChar = new Playable(Name, jobName); should have been
PlayableChar mainChar = new PlayableChar (Name, jobName);
//Note: Capitalisation is not recommended for variable names in java.
//Referring to variable *Name* here
b)中 引用fireBall(ennemy)应该是fireBallAttack(ennemy)。
当你打电话时,你正在将PlayableChar类中的可播放属性从Wizard / Warrior缩小到Char类 mainChar.getPlayable()。fireBallAttack(ennemy)。 mainChar.getPlayable()。fireBallAttack(ennemy)返回一个Char。在这个阶段,编译器无法知道它的向导/战士。它只知道,它是Char类的一个实例。方法 fireBallAttack(ennemy)仅在Warrior类中定义,而不在Char类中定义。这会导致编译器抛出编译时错误。
这更像是一个设计问题,有多种解决方案可以解决这个问题。最简单的方法是将以下两种方法添加到Char类中。
public void fireBallAttack(Char ennemy)
{
}
public void swordAttack(Char ennemy)
{
}
答案 1 :(得分:0)
The problem is that getHero()
(which isn't defined and you probably mean getPlayable()
) returns a Char
. Thus the compiler doesn't know the actual type (which could change at runtime) and thus doesn't know about the fireBall()
method.
You could try and add an abstract method attack(Char enemy)
to Char
and implement it accordingly in Wizard
and Warrior
.
Besides that, games are quite a complex topic, so don't try to do too much if you're just learning the language. Start with simple things and expand on those. In a complex enough game composition would probably be a better way to go, i.e. the characters would get a set of behavior/skill classes which represent the type of attack etc. But I'd consider such a design out of scope for now (I just mentioned it to give a hint on how you could improve later, for a learner your design is actually quite good :) ).