0

当我运行它时,它返回的值是 currentStation 的两倍加上增量值。而不仅仅是 currentStation 加上增量值。我无法弄清楚它是 AutoRadioSystem 还是 Radio 类中的问题。

public class AutoRadioSystem
{

  private Radio selectedRadio;
  private AMRadio radioAM = new AMRadio();
  private FMRadio radioFM = new FMRadio(); 
  private XMRadio radioXM = new XMRadio();

  public AutoRadioSystem()
  {
    selectedRadio = new AMRadio();
  }
  public double getCurrentStation()
  {
    if (selectedRadio.equals(radioAM))
    {
      return radioAM.getCurrentStaion();
    }
    else if (selectedRadio.equals(radioFM))
    {
      return radioFM.getCurrentStaion();
    }
    else if (selectedRadio.equals(radioXM))
    {
      return radioXM.getCurrentStaion();
    }
    return 0.0;
  }
  public void selectRadio()
  {
    if (selectedRadio.equals(radioAM))
      selectedRadio = radioFM;
    else if (selectedRadio.equals(radioFM))
      selectedRadio = radioXM;
    else if (selectedRadio.equals(radioXM))
      selectedRadio = radioAM;  
  }

  public void upCategory()
  {
    double catUp = radioXM.getCurrentStaion();
    catUp += 10;
    if (catUp > 199.0)
    {
      catUp = 1; 
      radioXM.setCurrentStation(catUp);
    }
    radioXM.setCurrentStation(catUp);
  }

  public void up()
  {
   if (selectedRadio.equals(radioAM))
    {
      double stationUp = radioAM.getCurrentStaion();
      stationUp += radioAM.getIncrement();
      radioAM.setCurrentStation(stationUp);
      // System.out.println(stationUp );
  }
  }
  public boolean equals (Object o) 
  {
    if (o == null) 
      return false; 
    if (! (o instanceof AutoRadioSystem)) 
      return false; 
    AutoRadioSystem other = (AutoRadioSystem) o; 
    return this.selectedRadio == other.selectedRadio;
  }

  public static void main (String [] args) { 
    AutoRadioSystem c = new AutoRadioSystem();
    c.selectRadio();
    double b = c.getCurrentStation();
    System.out.println(b);
    c.selectRadio();
    double d = c.getCurrentStation();
    System.out.println(d);
    c.upCategory();
    double f = c.getCurrentStation();
    System.out.println(f);
    c.selectRadio();
    double e = c.getCurrentStation();
    System.out.println(e);
    c.up();
    double g = c.getCurrentStation();
    System.out.println(g);
    c.up();
    double t = c.getCurrentStation();
    System.out.println(t);
  }
}

附加代码

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

}


public abstract class Radio 
{
 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)
 {
   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;
  }
4

1 回答 1

2

问题是以下方法:

public void setCurrentStation(double freq)
 {
   // This is equal to currentStation = currentStation + freq
   currentStation += freq;
 }

它应该是

public void setCurrentStation(double freq)
 {
   currentStation = freq;
 }

因为您已经添加currentStationup方法。

只是建议,与问题无关:

  • 想想“selectRadio”方法。拥有它的目的是什么。
  • 您还拥有Radio该类的 as 属性和其他三个实现。我认为您应该考虑使用“实例”运算符。
于 2013-09-25T01:54:09.923 回答