我在我的介紹課程中有一個關於實現的問題。我想出了一個答案,但彙編說,「編譯錯誤(第3行,第9列):可能的精度損失」我很困惑這種精度的損失是什麼。
我的家庭作業的問題如下: 回想一下,Person類實現了Comparable接口:Java的實現幫助介紹
public class Person implements Comparable
現在假設我們想通過他們的工資比較的員工。由於Employee擴展了Person,Employee已經通過Person compareTo方法實現了Comparable,Person compareTo方法按年齡比較Person對象。現在我們想要重寫Employee類中的compareTo方法,以便進行薪水比較。
對於此分配,通過爲該類實施新的compareTo方法來修改Employee類。在下面提供的空白處輸入適當的代碼,這樣如果員工A的工資低於員工B的工資,則認爲員工A被認爲少於員工B.此外,如果員工A的工資等於員工B的工資,那麼他們應該是平等的。請記住,您輸入的代碼位於Employee類中。
/**
* Compares this object with the specified object for order.
* @param o the Object to be compared.
*/
public int compareTo(Object obj)
{
這裏是我的代碼
double b= ((Employee)obj).getSalary();
double a= this.salary;
return(a-b);
}
這裏是Employee類代碼:
class Employee extends Person
{
private double salary;
/**
* constructor with five args.
* @param n the name
* @param ag the age
* @param ht the height
* @param p the phone number
* @param the salary
*/
public Employee(String n, int ag, int ht, String p, double s)
{
super(n, ag, ht, p);
salary = s;
}
/**
* Get the salary.
* @return double the salary.
*/
public double getSalary()
{
return salary;
}
/**
* Raise the employee's salary by a given percent.
* @param percentRaise
*/
public void raise(double percentRaise)
{
salary *= (1 + percentRaise);
}
/**
* Compares this object with the specified object for order.
* @param o the Object to be compared.
*/
public int compareTo(Object obj)
{
/* your code goes here */
}
/**
* get a String representation of the employee's data.
* @return String the representation of the data.
*/
public String toString()
{
return super.toString() + " $" + getSalary();
}
}
任何幫助,讓我正確回答將是非常讚賞。我一直在研究這個單獨的問題超過一個小時,而編譯錯誤令我困惑不已。謝謝!
非常感謝你,我傻。我知道有些東西與迴歸和類型有關,但我無法弄清楚。這是一個巨大的幫助。謝謝 –