我开始开发我的第一个java项目,它是一种基本的角色扮演游戏。我想创建一个角色。
首先我会解释一些事情:
Game
class - 获取字符类输入(如战斗机)。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;
}
}
答案 0 :(得分:2)
使您的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,玩家将拥有其他各种可能性,例如在他的库存中存储物品,与世界互动等等。
祝你好运!