【问题标题】:Equals method with oop using java使用java的带有oop的equals方法
【发布时间】:2011-03-19 03:59:21
【问题描述】:

我需要编写一个返回 true 的方法 o 是四分卫,并且 o 的名字、姓氏、尝试次数、完成次数、码数、拦截和达阵都等于该四分卫的相应属性。这就是我得到的,我坚持使用这种 equals 方法。有人可以让我开始使用它吗,我是新手

公开课四分卫 {

private int attempts;
private int completions;
private String firstName;
private int interceptions;
private String lastName;
private int touchdowns;
private int yards;
//************************************************************
public Quarterback()
{
    new Quarterback();
}

//****************************************************************

public Quarterback(String firstName, String lastName, int completions, int attempts, int yards, int interceptions, int touchdowns)
{
    this.firstName = firstName;
    this.lastName = lastName;
    this.completions = completions;
    this.attempts = attempts;
    this.yards = yards;
    this.interceptions = interceptions;
    this.touchdowns = touchdowns;

}

   //*****************************************************************

public Quarterback copy()
{
    Quarterback o = new Quarterback();
    o.firstName = this.firstName;
    o.lastName = this.lastName;
    o.completions = this.completions;
    o.attempts = this.attempts;
    o.yards = this.yards;
    o.interceptions = this.interceptions;
    o.touchdowns = this.touchdowns;
    return o;



}

    //******************************************************************    

public boolean equals(Object o)
{

}

    //*********************************************************************

public int getAttempts()
{
    return this.attempts;
}

  //*******************************************************************************

public int getCompletions()
{
    return this.completions;
}

  //*******************************************************************************

public String getFirstName()
{
    return this.firstName; 
}

   //*******************************************************************************

public int getInterceptions()
{
    return this.interceptions;

}

   //****************************************************************************   

public String getLastName()
{
    return this.lastName;
}
 //****************************************************************************

public void getRating()
{

}

 //**************************************************************************** 

public int getTouchdowns()
{
    return this.touchdowns;

}

//*****************************************************************************

public int getYards()
{
    return this.yards;
}

 //*******************************************************************************  

public void setAttempts(int attempts)
{
    this.attempts = attempts;

}

 //*******************************************************************************

public void setCompletions(int completions)
{
    this.completions = completions;

}

 //*******************************************************************************

public void setFirstName(String firstName)
{
    this.firstName = firstName;

}
 //*******************************************************************************

public void setInterceptions(int interceptions)
{
    this.interceptions = interceptions;
}
 //*****************************************************************************    

public void setLastName(String lastName)
{
this.lastName = lastName;
  //*****************************************************************************   

public void setTouchdowns(int touchdowns)
{
    this.touchdowns = touchdowns;
}
  //*****************************************************************************   

public void setYards(int yards)
{
    this.yards = yards;
}
 //*****************************************************************************

public String toString()
{

}

}

【问题讨论】:

  • 您已经在上一个问题中得到了关于如何实现equals 的答案。提示 - 当您提出关于 SO 的问题时...阅读答案

标签: oop methods equals


【解决方案1】:
public boolean equals(Object o)
{
    if(!o instanceof Quarterback)
        return false;
    Quarterback q = (Quarterback)o;
    return this.firstName.equals(q.getFirstName()) && this.lastName.equals(q.getLastName()) && this.attempts == q.getAttempts() && {the rest of the variables};
}

【讨论】:

    【解决方案2】:

    假设您想将 Quarterback 对象传递给您的 equals 函数,您必须调用不同的 get 函数来获取 o 的相应信息,然后将其与对象本身进行比较。它看起来像这样。

    public boolean equals(Object o)
    {
        if !(o.getAttempts() == this.attempts) return false;
    }
    

    这只是一个让你开始的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 2020-01-30
      • 2019-08-30
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多