如何计算两个对象之间的年龄差异?

时间:2021-04-18 15:40:20

标签: java

我正在学习JAVA OOP,我必须比较2个对象之间的年龄:

在java Procedural中,我会做到:

public static int calculateDifferenceAge(int agePlayer1, int agePlayer2){
      int differenceAge = agePlayer1 - agePlayer2;

      if(differenceAge < 0){
        differenceAge = -differenceAge;
      }
      return differenceAge; 
}

然后

public static void displayDifferenceAge(String namePlayer1, String namePlayer2, int agePlayer1, int agePlayer2){
      System.out.println("Age difference between " + namePlayer1 + " and " + namePlayer2 + " is of" + calculateDifferenceAge(agePlayer1, agePlayer2) + " year(s).");
    }
}

我不明白如何在 OOP 中创建我的 calculateDifferenceAge() 方法?

在我的主文件中,我有这个:

List<Player> players = new ArrayList <Player>();
players.add(new Player("Eric", 31, true));
players.add(new Player("Juliette", 27, false));

我坚持我的两种方法:

如何减去2个物体的年龄?

public static int calculateAgeDifference(List <Player> players){

    Player differenceAge = (players.get(0) - players.get(1));

    return differenceAge; 

  }

  public static void displayCalculateAgeDifference(List <Player> players){
    System.out.println(calculateAgeDifference().age);
  }

类玩家

public class Player {

  public String name;
  public int age;
  public boolean sex;

  public Player(String name, int age, boolean sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
  }

3 个答案:

答案 0 :(得分:3)

让我们从类 function Parent() { const [ arr, setArr] = useState( <some big array> ); let handleChange = e => { // logic to figure out what needs to be updated setArr(<new array>); } return ( { arr.map( (el, i) => <Child prop1={el} onChange={handleChange}> ) } ) } 开始。让我们给它一个 Player 和一个 name,以及一个 age 方法。它应该看起来像,

calculateAgeDifference

然后你可以这样称呼它

public class Player {
    private int age;
    private String name;

    public Player(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int calculateAgeDifference(Player player2) {
        return Math.abs(this.age - player2.age);
    }
}

您必须有一个类似的 Player a = new Player("Eric", 40); Player b = new Player("Sally", 51); System.out.println(a.calculateAgeDifference(b)); 类。您的似乎也有一个 Player 字段。不清楚为什么。所以我不能说。

答案 1 :(得分:3)

您在代码中只遗漏了一小步。提取年龄列表的步骤应该是:

1.- 从列表中提取对象

2.- 提取该物体(或玩家,在本例中)的年龄

3.- 减去年龄

有一些方法可以做到,但我会这样做:

Warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering.

希望有帮助。我所做的是从列表中提取对象播放器:players.get(0) 提取列表中的第一个对象,即播放器。现在我有一个玩家并且它有一个年龄变量,我必须用 player.age 提取它。我折叠了这些步骤,如果您有任何问题,我可以进一步解释

显示方式:

public static int calculateAgeDifference(List<Player> players) {
    int age1= players.get(0).age;
    int age2= players.get(1).age;

    int differenceAge = age1 - age2;

    if(differenceAge < 0){
        differenceAge = -differenceAge;
    }
    return differenceAge;
}

答案 2 :(得分:2)

为什么你的方法接口从两个参数变成了一个列表?您仍然可以传递对象的两个实例。您仍然可以从方法中返回整数年龄值,无需创建仅保存年龄的 Frankenstein's Player 实例。

我假设您的 Player 类有一个方法 getAge() 来提取在构造函数中传入的年龄值:

public static int calcAgeDiff(final Player player1, final Player player2) {
  int age1 = player1.getAge();
  int age2 = player2.getAge();
  return Math.abs(age2 - age1);
}

或者,您可以向 Player 类本身添加一个实例方法来计算不同玩家的年龄差异:

public class Player {
  // fields
  // constructor
  // getters
  public int ageDiffTo(final Player otherPlayer) {
    return Math.abs(this.age - otherPlayer.age); // <- a class can always access its private fields, even of other instances
  }
}

然后调用 player1.ageDiffTo(player2)