简体   繁体   English

我如何编写这两种不同的方法并对其进行测试

[英]How do i write these two different methods and test them

Write two methods called isBetterThan a.写两个方法叫做 isBetterThan 一个。 The first one should take an int pts and return true if the player has more than pts points and false otherwise.第一个应该采用 int pts 并在玩家拥有超过 pts 点时返回 true,否则返回 false。 b.The second method should take another Player object and return true if this player has more points than the other player and false otherwise.第二种方法应该采用另一个 Player 对象,如果该玩家比其他玩家拥有更多点数,则返回 true,否则返回 false。 c. C。 In a comment after this method explain why you cannot create another isBetterThan method which takes another player object and returns the string “yes” if this player has more points than the other player and the string “no” otherwise.在此方法之后的评论中解释为什么不能创建另一个 isBetterThan 方法,该方法接受另一个玩家对象并返回字符串“yes”,如果该玩家比其他玩家拥有更多积分,否则返回字符串“no”。

I have done the first one but do not know how to do the second one.我做了第一个,但不知道如何做第二个。 I do not know how to call a new Player object as the input.我不知道如何调用一个新的 Player 对象作为输入。

public class Player{
    private String name;
    private int points;
    private int level;
    public static final int MAX_LEVEL = 10;
    public static int finished = 0;

  public boolean isBetterThan(int value){
        if(value>points)
            return true;
        else
            return false;
     }

a method that would give me another isbetterthan method... I already have a main method that calls these methods for testing.一种能给我另一种比方法更好的方法......我已经有一个调用这些方法进行测试的主要方法。

Here:这里:

public boolean isBetterThan(int value){

allows you to do: somePlayer.isBetterThan(5) for example.允许您执行以下操作:例如somePlayer.isBetterThan(5)

Now simply go in and add现在只需进入并添加

public boolean isBetterThan(Player otherPlayer){
  if (this.points <= otherPlayer.points) ... 

and add the kind of comparison/decision making you want to happen.并添加您想要发生的比较/决策类型。 In order to avoid code duplication, you could simply implement the method to为了避免代码重复,您可以简单地实现该方法以

  return isBetterThan(otherPlayer.points)

But that leads to the question why you got two APIs doing similar things.但这引出了一个问题,为什么你有两个 API 做类似的事情。 I would it find more reasonable to only other a isBetterThan(Player) method.我认为只有其他isBetterThan(Player)方法更合理。 You see, your API that compares against a simple int value basically externalizes that view about "levels and points" to the outside world.你看,你的 API 与一个简单的 int 值进行比较,基本上将关于“级别和点”的观点外化到外部世界。 So either the actual "is better than" is something that is really based on just that number, or it is more complicated.因此,要么实际的“优于”实际上仅基于该数字,要么更复杂。

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

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