1

selectedRadio を AM FM と XM の間で比較して、現在どちらがオンになっているかを判断し、特定のラジオのステーションを返す方法を見つけようとして問題が発生しました。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

1

などのequalsすべてのサブクラスでメソッドを定義します。これには Apache commons から使用できます。http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/EqualsBuilder.htmlAMRadioFMRadioEqualsBuilder

代表的な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 に答える