我试图找出为什么我不断收到我的 AM 类没有覆盖抽象方法的错误。在我老师的 UML 图中,它只显示我需要父 radio 类中的 equals (Object o) 方法。另外,我没有在我的抽象类中将其声明为抽象。
public abstract class Radio implements Comparable
{
double currentStation;
RadioSelectionBar radioSelectionBar;
public Radio()
{
this.currentStation = getMin_Station();
}
public abstract double getMax_Station();
public abstract double getMin_Station();
public abstract double getIncrement();
public void up()
{
}
public void down()
{
}
public double getCurrentStaion()
{
return this.currentStation;
}
public void setCurrentStation(double freq)
{
this.currentStation = freq;
}
public void setStation(int buttonNumber, double station)
{
}
public double getStation(int buttonNumber)
{
return 0.0;
}
public String toString()
{
String message = ("" + currentStation);
return message;
}
public boolean equals (Object o)
{
if (o == null)
return false;
if (! (o instanceof Radio))
return false;
Radio other = (Radio) o;
return this.currentStation == other.currentStation;
}
public static void main(String[] args)
{
Radio amRadio = new AMRadio();
System.out.println(amRadio);
Radio fmRadio = new FMRadio();
System.out.println(fmRadio);
Radio xmRadio = new XMRadio();
System.out.println(xmRadio);
}
}
public class AMRadio extends Radio
{
private static final double Max_Station = 1605;
private static final double Min_Station = 535;
private static final double Increment = 10;
public AMRadio()
{
currentStation = Min_Station;
}
public double getMax_Station()
{
return this.Max_Station;
}
public double getMin_Station()
{
return this.Min_Station;
}
public double getIncrement()
{
return this.Increment;
}
public String toString()
{
String message = ("AM " + this.currentStation);
return message;
}
}
最佳答案
您必须实现 compareTo()
方法,假设 Radio
实现了 Comparable
接口(interface),并且 Radio
类中没有提供此方法的具体实现,因此您有两个选择:
- 在
Radio
的所有子类中实现compareTo()
- 或者在
Radio
中实现compareTo()
类似这样的内容,在 AMRadio
中:
public int compareTo(AMRadio o) {
// return the appropriate value, read the linked documentation
}
或者像这样,在Radio
中:
public int compareTo(Radio o) {
// return the appropriate value, read the linked documentation
}
关于java - java中的抽象类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18990639/