1

我在试图弄清楚如何比较 AM FM 和 XM 之间的 selectedRadio 时遇到问题,以便我可以确定它当前在哪个电台上,然后返回特定电台的电台。我知道我需要 equals 方法我只是不确定使用它的正确方法来获得我正在寻找的结果。

public class AutoRadioSystem
    {
      private Radio selectedRadio;
      private AMRadio radioAM;
      private FMRadio radioFM; 
      private XMRadio radioXM;

      //I'm not sure if this is the correct way to do this so that selectedRadio equals the object of radioAM
      public AutoRadioSystem()
      {
        selectedRadio = radioAM;
      }
     //trying to figure out how to compare if selectedRadio is one of these. 
      public double getCurrentStation()
      {
        if (selectedRadio == radioAM)
        {
          return radioAM.getCurrentStaion();
        }
        else if (selectedRadio == radioFM)
        {
          return radioFM.getCurrentStaion();
        }
        return 0.0;
      }
      // its supposed to switch the radio from AM to FM when this method is called 
      public void selectRadio()
      {
        if (selectedRadio.equals(radioAM))
          selectedRadio = radioFM;
      }
      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);
      }
    }


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)
 {
   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 FMRadio extends Radio
{
  private static final double Max_Station = 108.0;
  private static final double Min_Station = 88.0;
  private static final double Increment = .01;
  public FMRadio()
  {
    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 = ("FM " + this.currentStation);
    return message;
  } 
} 
4

2 回答 2

2

要点:(从帖子底部移动)

您需要了解测试ObjA == ObjBObjA.equals(ObjB)和之间的区别ObjA instanceof ClassB


这种方法

public double getCurrentStation()
{
    if (selectedRadio == radioAM)
    {
        return radioAM.getCurrentStaion();
    }
    else if (selectedRadio == radioFM)
    {
        return radioFM.getCurrentStaion();
    }
    return 0.0;
}

可能不会像你期望的那样工作。如果您double getCurrentStation()在 Radio 基类中实现该方法(您已经这样做了),您可以执行以下操作:

public double getCurrentStation()
{
    return selectedRadio.getCurrentStation();
}

但这可能是矫枉过正,所以你应该把getCurrentStation()调用AutoRadioSystem替换为selectedRadio.getCurrentStation().

相似地,

public void selectRadio()
{
    if (selectedRadio.equals(radioAM))
        selectedRadio = radioFM;
}

不会像你期望的那样工作。你可能想要类似的东西

public void selectRadio()
{
    if (selectedRadio. instanceOf AMRadio))
        selectedRadio = new FMRadio();
}

还,

if (o == null)           
    return false;        
if (! (o instanceof AutoRadioSystem))           
    return false;  

可以简化为

if (! (o instanceof AutoRadioSystem))
    return false;

由于o instanceof SomeClass返回falseif ois null

还有其他问题。

于 2013-09-24T20:24:27.700 回答
1

在所有子类中定义equals方法AMRadioFMRadio等等。您可以使用EqualsBuilderApache commons 来实现这一点。http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/EqualsBuilder.html

使用的典型equals方法EqualsBuilder如下所示。它使用反射来比较相等性。

 public boolean equals(Object obj) {
     return EqualsBuilder.reflectionEquals(this, obj);
  }

并且在比较时不要使用,==因为它检查引用相等,而不是逻辑相等。使用equals方法如

   if (selectedRadio.equals(radioAM))
    {
      return radioAM.getCurrentStaion();
    }
于 2013-09-24T20:21:04.187 回答