简体   繁体   中英

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.

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.

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:

 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.


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.
  • Is there a reason why you are using wrapper-objects instead of primitives? Especially with type conversion, this can lead to ugly situations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM