我正在尝试创建一个角色在游戏中的轮廓。
然后我想创建10个不同的角色,并在不同级别比较他们的统计数据。
我正在创建一个名为Character
的类,看起来像这样,
public class Character {
String primaryAtribute;
double strength;
double agility;
double inteligence;
double armor;
double damageMultiplyer;
int health;
int mana;
int range;
int movement;
int level;
int damageMin;
int damageMax;
public Character() {
} //constructor ends
private static int randomInt(int min, int max) {
return min + (int)(Math.random() * (max - min + 1));
}
int damageRange = randomInt(damageMin, damageMax);
double damage = damageRange + level * damageMultiplyer;
} //class ends
然后我创建这样的每个角色,
public class FirstCharacter {
String primaryAtribute = "Agility";
int level = 0;
double strength = 17 + level * 1.9;
double agility = 26 + level * 1.9;
double inteligence = 15 + level * 1.4;
double armor = 0.64;
double damageMultiplyer = 1.9;
int health = 473;
int mana = 195;
int range = 625;
int movement = 300;
int damageMin = 44;
int damageMax = 55;
}
我在正确的道路上吗?
一旦我创建了每个角色的课程,我将如何设置我可以选择哪个Character
课程?
答案 0 :(得分:2)
正如其他人所推荐的那样,你应该只读一本关于OOP的书。但是,简单地说:
您提供的代码会创建两个完全不相关的对象类型Character
和FirstCharacter
。但是,看起来你真正想做的是创建一个字符类的实例,如下所示:
Character firstChar = new Character();
然后使用访问器方法或其他方法来设置角色的属性。例如,如果字段是公开的(不推荐),您可以这样做:
firstChar.strength = 17;
firstChar.agility = 26;
...
您需要在级别更改后重新计算这些值,不能按照您尝试的方式为它们指定公式。
祝你好运!答案 1 :(得分:2)
正如我之前的评论所推荐,你应该事先了解一些OO原则。我会尝试概述一些可以让你走上正确轨道的方法。
接口:
interface Character {
// define methods that must be defined across all character types
public void getLevel();
public void getAgility();
...
}
实施此界面:
public class Bob implements Character {
private String primaryAtribute;
private double strength;
private double agility;
private double inteligence;
private double armor;
private double damageMultiplyer;
private int health;
private int mana;
private int range;
private int movement;
private int level;
private int damageMin;
private int damageMax;
// default constructor
public Bob() {}
// override as many times as you'd like
public Bob(int level) {
this.level = level;
}
// getters and setters - required to implement ALL from interface
public void getLevel() {
return this.level;
}
public void getAgility() {
return this.agility;
}
public void setAgility(double agility) {
this.agility = agility;
}
}
从这里开始,我们可以将Bob用作我们喜欢的新实例:new Bob(...);
。
对于封装,大多数成员变量始终是私有的。这完全取决于您的用例以及您希望如何构建程序。例如,您可以将health
更改为public int health
,并且您可以在没有getHealth()
功能的情况下访问此项,您可以直接调用{{ 1}}。
我简要介绍的主题是类变量,接口,范围,重载,可能还有其他内容。您需要拿起一本书,甚至阅读Java文档。他们有一些很好的例子,而且非常冗长。
答案 2 :(得分:2)
很抱歉迟到,交通是一场噩梦。以下是您可以做的事情的可能性:
public class Character {
// ...
public Character(String primaryAttribute, int level /* , ... */){
this.primaryAttribute = primaryAttribute;
this.level = level;
// Write all fields with constructor variables.
}
}
从那里你可以使用:
{
Character myfirstcharacter = new Character("Agility", 0 /* , ... */);
}
public class FirstCharacter extends Character {
{
primaryAtribute = "Agility";
level = 0;
strength = 17 + level * 1.9;
agility = 26 + level * 1.9;
inteligence = 15 + level * 1.4;
armor = 0.64;
damageMultiplyer = 1.9;
health = 473;
mana = 195;
range = 625;
movement = 300;
damageMin = 44;
damageMax = 55;
}
}
这是一个扩展你的Character
类并在构造时写入其字段的类。
{
Character myfirstcharacter = new FirstCharacter();
// FirstCharacter myfirstcharacter = new FirstCharacter();
}
这就像选项 B :
{
Character myfirstcharacter = new Character() {
{
primaryAtribute = "Agility";
level = 0;
strength = 17 + level * 1.9;
agility = 26 + level * 1.9;
inteligence = 15 + level * 1.4;
armor = 0.64;
damageMultiplyer = 1.9;
health = 473;
mana = 195;
range = 625;
movement = 300;
damageMin = 44;
damageMax = 55;
}
}
}