[英]Is there a way to implement a method on a child class that I can access the attributes from it's child?
I'm trying to build an app to learn Java, basically it's a Football Match on console. 我正在尝试构建一个学习Java的应用,基本上这是在控制台上进行的足球比赛。
The Class Player has all the attributes like name, number, age.. There are other classes which extends from Player, they are the Player Roles (Striker, Defender, Midfielder, Goalkeeper). 班级球员具有姓名,号码,年龄等所有属性。还有其他一些从球员扩展的类别,即球员角色(前锋,后卫,中场,守门员)。 The Roles implements a Interface that has the methods to get each one it's own attribute formula (Like Defense Power and Attack Power).
角色实现了一个接口,该接口具有获取每个属性的方法(如防御力和攻击力)。 So then there's a method which is the General Power, this one's formula is equal to everyone, independent to the player's role.
因此,这里有一种方法,即“通用权力”,该公式等于每个人,独立于玩家的角色。
If I set the General Power in the child's class methods, I'll have to write in each Role Class the same code, several times (I'm almost sure that it's wrong.. code reuse). 如果在子类的类方法中设置“通用权限”,则必须在每个角色类中编写相同的代码几次(我几乎可以肯定这是错误的。代码重用)。
Maybe I've to implement the method on the interface, but I've no idea how to proceed. 也许我必须在接口上实现该方法,但是我不知道如何进行。
I've got a link on GitHub - It's on the Develop Branch. 我在GitHub上有一个链接-在Develop分支上。
And here below is some code to try to understand what I'm trying to figure out. 下面是一些代码,这些代码试图了解我要弄清楚的内容。
public interface PlayerAttributes {
PlayerPosition getPlayerPosition(PlayerPosition position);
Double getAttackPower(Float value);
}
class Player {
String name;
Integer number;
PlayerPosition position; //Enum
}
public class Defender extends Player implements PlayerAttributes {
@Override
public PlayerPosition getPlayerPosition(PlayerPosition position) {
return PlayerPosition.DEFENDER;
}
@Override
public Double getAttackPower(Float value) {
return value * 1.2;
}
@Override
public Double getDefensePower(Float value) {
return value * 2.5;
}
}
The following answer is just a suggestion and by far not the only solution possible. 以下答案仅是一个建议,并不是迄今为止唯一的解决方案。 It uses abstract classes by letting
Player
implement the PlayerAttribute
interface: 它通过让
Player
实现PlayerAttribute
接口来使用抽象类:
public interface PlayerAttributes {
PlayerPosition getPlayerPosition(PlayerPosition position);
Float getAttackPower(Float value);
Float getDefensePower(Float value);
}
abstract class Player implements PlayerAttributes {
String name;
Integer number;
@Override
public PlayerPosition getPlayerPosition(PlayerPosition position) {
return PlayerPosition.DEFENDER;
}
@Override
public Float getAttackPower(Float value) {
return value * attackPowerMultiplier();
}
public Float getDefensePower(Float value) {
return value * defensePowerMultiplier();
}
public abstract Float attackPowerMultiplier();
public abstract Float defensePowerMultiplier();
}
class Defender extends Player implements PlayerAttributes {
public static final Float ATTACK_POWER_MUTLIPLIER = 1.2f;
public static final Float DEFENSE_POWER_MUTLIPLIER = 2.5f;
@Override public Float attackPowerMultiplier() {
return ATTACK_POWER_MUTLIPLIER;
}
@Override public Float defensePowerMultiplier() {
return DEFENSE_POWER_MUTLIPLIER;
}
}
enum PlayerPosition {
DEFENDER
}
The get<Type>PowerMultiplier()
-methods will still be pretty similar, but this approach tries to minimize repetition. get<Type>PowerMultiplier()
方法仍将非常相似,但是这种方法试图使重复最小化。
Two remarks on your code: 关于您的代码的两个说明:
float
, it should return a float
(same for double
) unless it is blatantly obvious that a conversion is necessary or intended. float
,则它应该返回一个float
(与double
相同),除非明确地表明有必要或打算进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.