逻辑错误可能在Java分配中引起误解 [英] Logic error possibly misunderstanding in java assignment

查看:75
本文介绍了逻辑错误可能在Java分配中引起误解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使该项目正常工作方面,我遇到了很多问题,但是我目前仍在让该类正常工作.它想做的是从无线电类中获取当前电台,并将其传递给该类.问题是我试图在AM和FM之间进行选择,但是每次我运行它时,它只会显示AM电台.我不明白为什么它会自动设置为该电台.

I've been having numerous problems getting this project to work correctly but I'm currently stuck on getting this class to work properly. Whats its suppose to do is take the current station from the radio class and pass it along to this class. The problem is i'm trying to select between AM and FM but every time i run it, it only displays the AM station. I don't understand why it automatically gets set to that station.

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

  //is this the correct place to initialize these? 
  Radio amRadio = new AMRadio();
  Radio fmRadio = new FMRadio();
  public AutoRadioSystem()
  {
   //even making the selected radio FM still produces values for AM
   selectedRadio = radioFM;
  }

  // this is where my problem currently lies and probably much more. Shouldn't it return 0.0 without any station being selected.  
  public double getCurrentStation()
  {
    if (selectedRadio == radioAM)
    {
      return amRadio.getCurrentStaion();
    }
    else if (selectedRadio == radioFM)
    {
      return fmRadio.getCurrentStaion();
    }
    return 0.0;
  }

  //I'm not sure if i'm setting this up correctly to switch the radio from am to fm 
  public void selectRadio()
  {
    if (selectedRadio == radioAM)
      selectedRadio = radioFM;
  }
  public static void main (String [] args) { 
    AutoRadioSystem c = new AutoRadioSystem();
    c.selectRadio();
    double b = c.getCurrentStation();
    System.out.println(b);
  }
}

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;
  } 
}

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;
  } 
}


public abstract class Radio
{
 double currentStation;

 RadioSelectionBar radioSelectionBar;
 public Radio()
 {

 }
 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;
  } 
 }

推荐答案

我认为我已经找到问题了

I think I've found the problem

每个Radio重载都有2个字段

private AMRadio radioAM;
...
Radio amRadio = new AMRadio();

,但是您要与之进行比较的那个对象:radioAM永远不会被实例化,因此始终为空.

but the one you're comparing to: radioAM never gets instantiated, and therefore is always null.

致电时

if (selectedRadio == radioAM)

selectedRadioradioAM都是null,因此它们当然是相等的

both selectedRadio and radioAM are null, so of course they would be equal

除非您打算将radioAMamRadio设置为完全不同的实例,否则不应有2个这样的字段.

unless you intend radioAM and amRadio to be completely different instances, than you shouldn't have 2 fields like that.

由于您使用的是多态性,因此您可能要使用后一种情况

Since you're using polymorphism, you might want to use the latter one

Radio amRadio = new AMRadio();

这篇关于逻辑错误可能在Java分配中引起误解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆