简体   繁体   English

有没有一种方法可以在子类上实现可以从子类访问属性的方法?

[英]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: 关于您的代码的两个说明:

  • You go a little bit wild with floating-poinnt types. 您对浮点类型有点疯狂。 In general, if a method accepts a float , it should return a float (same for double ) unless it is blatantly obvious that a conversion is necessary or intended. 通常,如果一个方法接受一个float ,则它应该返回一个float (与double相同),除非明确地表明有必要或打算进行转换。
  • Is there a reason why you are using wrapper-objects instead of primitives? 为什么要使用包装对象而不是基元? Especially with type conversion, this can lead to ugly situations. 尤其是使用类型转换时,这可能会导致难看的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从未被子类覆盖的方法内部访问子对象的字段吗? - Can I have access to a field of a child object from inside a method which is not overridden by the child class? 从另一个子类访问子方法 - Access a child method from another child class 如何在JAVA中从父类的内部类中调用子类中的重写方法? - How can I call the overridden method in a child class from a parent class's inner class in JAVA? 从C#中的子类访问父方法 - Access Parent's method from child class in C# 当父类是引用类型并通过方法传递时,如何从子类访问属性? - How can I access an attribute from a child class when the parent class is the reference type and passed through a method? 当孩子被归类为父母时,我可以访问孩子的方法吗? - Can I access a child's method when it is classified as a parent? 如何从具体子类中的重写方法访问通过抽象类中的构造函数实例化的对象的属性? - How to access the attributes of an object instantiated via a constructor in an abstract class, from an overridden method in a concrete child class? Java子类无法实现父类的抽象方法 - Java child class not able to implement parent class's abstract method 实施儿童班的好方法 - Good way to implement a child class 如何从抽象类强制实施子类以实现静态方法 - How do I enforce a child class from an abstract class to implement a static method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM