我在我的介绍课程中有一个关于实现的问题。我想出了一个答案,但汇编说,“编译错误(第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();
}
}
任何帮助,让我正确回答将是非常赞赏。我一直在研究这个单独的问题超过一个小时,而编译错误令我困惑不已。谢谢!
非常感谢你,我傻。我知道有些东西与回归和类型有关,但我无法弄清楚。这是一个巨大的帮助。谢谢 –