我正在研究一个简单的基于文本的rpg battler程序作为Java的介绍。我似乎对大多数代码有了不错的理解,但我遇到了几个问题。
我遇到的问题是在Project类中。
在我的switch语句中,我尝试使用setSpells()和setArrows()方法,我收到“找不到符号”错误消息。我意识到这可能是由于我在子类中设置错误,但我不确定那是什么。
第二个问题是在print语句中使用c.getName()来拉取字符名称。该语句的c部分给出了相同的错误消息。
在这些情况下,我是否有一些简单的误解?任何帮助解决这个问题将不胜感激。谢谢。
这是我的主要项目类文件:
package project;
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
System.out.println("Welcome to Lands of the Sun\n");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.print("Please choose your class (wizard or elf): \n");
String classChoice = sc.next();
sc.nextLine();
System.out.print("Please choose a name for your " + classChoice + ": ");
String charName = sc.next();
sc.nextLine();
int healthVal = (int) (Math.random() * 10) + 1;
switch (classChoice) {
case "wizard":
{
Character c = new Wizard();
c.setName(charName);
c.setGold(25);
c.setHealth(healthVal);
c.setSpells(10);
break;
}
case "elf":
{
Character c = new Elf();
c.setName(charName);
c.setGold(25);
c.setHealth(healthVal);
c.setArrows(10);
break;
}
}
System.out.print(c.getName());
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}
这是我的角色类:
package project;
public abstract class Character {
private String name;
private int gold;
private int health;
public static int count = 0;
public Character()
{
name = "";
gold = 0;
health = 0;
}
public Character(String name, int gold, int health) {
this.name = name;
this.gold = gold;
this.health = health;
}
public void setName(String name)
{
this.name = name;
}
public String getName(){
return name;
}
public void setGold(int gold)
{
this.gold = gold;
}
public int getGold()
{
return gold;
}
public void setHealth(int health)
{
this.health = health;
}
public int getHealth()
{
return health;
}
@Override
public String toString()
{
return "Name: " + name + "\n" +
"Gold: " + gold + "\n" +
"Health: " + health + "\n";
}
public static int getCount()
{
return count;
}
public abstract String getDisplayText();
}
这是我的向导子类:
package project;
public class Wizard extends Character {
private int spells;
public Wizard()
{
super();
spells= 0;
count++;
}
public void setSpells(int spells)
{
this.spells= spells;
}
public int getSpells(){
return spells;
}
@Override
public String getDisplayText()
{
return super.toString() +
"Spells: " + spells+ "\n";
}
}
最后是我的精灵子课:
package project;
public class Elf extends Character{
private int arrows;
public Elf()
{
super();
arrows = 0;
count++;
}
public void setArrows(int arrows)
{
this.arrows = arrows;
}
public int getArrows(){
return arrows;
}
@Override
public String getDisplayText()
{
return super.toString() +
"Arrows: " + arrows + "\n";
}
}
答案 0 :(得分:3)
当您创建一个Characters
...
Character c = new Elf();
您正在将实例向下转换为" act"与Character
类似,这在面向对象编程中非常有用,但在这种情况下会导致问题,因为Character
没有您正在寻找的方法。
相反,首先将类分配给实例的具体版本......
Elf elf = new Elf();
应用您需要的属性,然后将其分配给Character
引用...
Character c = null;
//...
switch (classChoice) {
//...
case "elf":
{
Elf elf = new Elf();
//...
c = elf;
}
}
c = elf;
例如......
有关详细信息,请查看Polymorphism部分
答案 1 :(得分:1)
你的问题是这些行
Character c = new Wizard();
....
Character c = new Elf();
字符类本身没有setFireballs或SetArrows方法。您需要将对象定义为向导或精灵才能访问所述方法...... EG:
Elf c = new Elf();
Wizard c = new Wizard();
etc
答案 2 :(得分:0)
字符c =新向导(); Character既没有setFireBalls方法也没有setArrows方法。
答案 3 :(得分:0)
我对您的代码进行了一些更改.. 这是最终的代码。
包h;
import java.util.Scanner;
公共课他{
public static void main(String[] args) {
System.out.println("Welcome to Lands of the Sun\n");
Character c = new Wizard();
Character e = new Elf();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.print("Please choose your class (wizard or elf): \n");
String classChoice = sc.next();
sc.nextLine();
System.out.print("Please choose a name for your " + classChoice + ": ");
String charName = sc.next();
sc.nextLine();
int healthVal = (int) (Math.random() * 10) + 1;
switch (classChoice) {
case "wizard":
{
c.setName(charName);
c.setGold(25);
c.setHealth(healthVal);
c.setFireballs(10);
break;
}
case "elf":
{
;
e.setName(charName);
e.setGold(25);
e.setHealth(healthVal);
e.setArrows(10);
break;
}
}
System.out.print(c.getName());
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}
向导类
公共类向导扩展了角色{ private int fireballs;
public Wizard()
{
super();
fireballs = 0;
count++;
}
public void setFireballs(int fireballs)
{
this.fireballs = fireballs;
}
public int getFireballs(){
return fireballs;
}
@Override
public String getDisplayText()
{
return super.toString() +
"Fireballs: " + fireballs + "\n";
}
@Override
public void setArrows(int i) {
}
}
精灵班
public class Elf extends Character { private int arrows;
public Elf()
{
super();
arrows = 0;
count++;
}
public void setArrows(int arrows)
{
this.arrows = arrows;
}
public int getArrows(){
return arrows;
}
@Override
public String getDisplayText()
{
return super.toString() +
"Arrows: " + arrows + "\n";
}
@Override
public void setFireballs(int i){
}
}
抽象类角色
公共抽象类Character { 私有字符串名称; 私人金; 私人健康; public static int count = 0;
public Character() {
name = "";
gold = 0;
health = 0;
}
public Character(String name, int gold, int health) {
this.name = name;
this.gold = gold;
this.health = health;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setGold(int gold) {
this.gold = gold;
}
public void setHealth(int health) {
this.health = health;
}
public int getHealth() {
return health;
}
@Override
public String toString() {
return "Name: " + name + "\n" + "Gold: " + gold + "\n" + "Health: "
+ health + "\n";
}
public static int getCount() {
return count;
}
public abstract String getDisplayText();
public abstract void setFireballs(int i);
public abstract void setArrows(int i);
}
希望这会有所帮助...