0

我试图弄清楚为什么我不断收到我的 AM 类没有覆盖抽象方法的错误。在我的教师 UML 图中,它只显示我需要在我的父无线电类中使用 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;
  } 
}
4

1 回答 1

2

你必须实现这个compareTo()方法,因为Radio实现了Comparable接口并且类中没有提供这个方法的具体实现Radio,所以你有两个选择:

  1. compareTo()在所有Radio的子类中实现
  2. compareTo()实施Radio

像这样的东西,在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
}
于 2013-09-24T19:45:18.843 回答