我开始开发我的第一个Java项目,这是一种基本的角色扮演游戏。我想创建一个角色。
首先,我将解释一些事情:Game
class-获取角色类输入(如Fighter)。Character
-具有所有字符数据。CharacterClass
-抽象类,其中包含每个字符类的信息(例如Fighter
)。Fighter
(和其他类)扩展了抽象类CharacterClass
,应为所选字符类(如Fighter
)设置初始状态。
我想做的是在Game
中选择一个字符类(假设我选择Fighter
),并通过displayStats
获取所有统计信息。当角色选择一个类别时,Game
应该调用类似的内容:Chracter ch = new Character(name, scan)
,然后是ch.displayCharacter
。
我的问题是Fighter
的构造函数,并将此数据分配给Character
的构造函数。
public class Game {
public static void main(String[] args) {
System.out.println("Choose a character: ");
System.out.println("1. Fighter");
System.out.println("2. Rogue");
System.out.println("3. Mage");
System.out.println("4. Cleric");
Scanner sc = new Scanner(System.in);
int scan = sc.nextInt();
System.out.println("Character Class: " + CharacterUtil.getCharacterClass(scan));
System.out.println("Choose Name:");
Scanner nameIn = new Scanner(System.in);
String name = nameIn.next();
System.out.println("Name: " + name);
}
}
public class Character {
private String name;
private String characterClass;
private int level;
private int hp;
private int currentHp;
private long xp;
/*private int BAB; /*Base attack bonus*/
private int strength;
private int constitution;
private int dexterity;
private int intelligence;
private int wisdom;
private int charisma;
Character(String name, String chracterClass){
this.name = name;
this.characterClass = chracterClass;
level = ;
hp = ;
currentHp = hp;
xp = 0;
strength = ;
constitution = ;
dexterity = ;
intelligence = ;
wisdom = ;
charisma = ;
}
void displayCharacter(){
System.out.println("Name: " + name);
System.out.println("Level: " + level);
System.out.println("Class: " + characterClass);
System.out.println("HP: " + hp);
System.out.println("Attributes: ");
System.out.println("Strength: " + strength);
System.out.println("Constitution: " + constitution);
System.out.println("Dexterity: " + dexterity);
System.out.println("Intelligence: " + intelligence);
System.out.println("Wisdom: " + wisdom);
System.out.println("Charisma: " + strength);
System.out.println("XP: " + xp);
}
}
abstract class CharacterClass {
private int level;
private int hp;
private int strength;
private int constitution;
private int dexterity;
private int intelligence;
private int wisdom;
private int charisma;
CharacterClass(){
level = 1;
hp = 10;
strength = 10;
constitution = 10;
dexterity = 10;
intelligence = 10;
wisdom = 10;
charisma = 10;
}
class Fighter extends CharacterClass {
Fighter(){
super.level = 1;
super.hp = 10;
super.strength = 16;
super.constitution = 14;
super.dexterity = 14;
super.intelligence = 10;
super.wisdom= 10;
super.charisma = 10;
}
}
最佳答案
可以使CharacterClass私有字段受保护,而不是私有字段,以便子类可以访问它们,或者在抽象类中实现getter / setter并将其用于子类构造函数中。
同样,super.field并不意味着任何事情:您正在实例化一个对象,该对象将具有抽象类和子类中的字段,您要访问的将是this.fields。
样例代码:
public abstract class CharacterClass {
private int intelligence;
private int strength;
private int dexterity;
private int vitality;
protected CharacterClass() {
setIntelligence(10);
setStrength(10);
setDexterity(10);
setVitality(10);
}
public int getDexterity() {
return dexterity;
}
protected void setDexterity(int dexterity) {
this.dexterity = dexterity;
}
public int getVitality() {
return vitality;
}
protected void setVitality(int vitality) {
this.vitality = vitality;
}
public int getStrength() {
return strength;
}
protected void setStrength(int strength) {
this.strength = strength;
}
public int getIntelligence() {
return intelligence;
}
protected void setIntelligence(int intelligence) {
this.intelligence = intelligence;
}
}
public class Fighter extends CharacterClass {
public Fighter() {
setStrength(15);
setVitality(15);
}
}
public class Main {
public static void main(String[] args) {
CharacterClass player = new Fighter();
System.out.println(player.getStrength());
System.out.println(player.getIntelligence());
}
}
这将先打印15,再打印10,因为强度已被figther修改,但是智能仍然是CharacterClass中定义的智能。
请注意,您可能希望您的玩家不只是CharacterClass或Figther。尽管您将有很多Figther,Rogues和Mages作为PNJ,但玩家将拥有其他可能性,例如将物品存储在库存中,与世界互动等。
祝您比赛顺利!