2011-03-19 58 views
0

我需要編寫一個返回true的o是四分衛的方法,o的名字,姓氏,嘗試次數,完成次數,碼數,攔截次數和達陣次數等於這個四分衛的相應屬性。這裏是我得到的,並堅持這種等於方法。有人可以讓我開始用它,即時通訊新本等於使用java的oop方法

公共類四分衛 {

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() 
{ 

} 

}

+0

您已經得到了關於如何在上一個問題中實現equals的答案。提示 - 當你問一個關於SO的問題時...... **閱讀答案**。 – 2011-03-19 07:42:37

回答

0
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}; 
} 
0

假設你想傳遞一個四分衛對象的平等工作,​​你會必須調用不同的get函數來獲取o的相應信息,然後將其與對象本身進行比較。它看起來像這樣。

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

這只是一個讓你在那裏開始的想法。