【发布时间】:2013-09-24 18:12:43
【问题描述】:
我在让这个项目正常工作时遇到了很多问题,但我目前一直在努力让这个课程正常工作。它的假设是从广播类中获取当前电台并将其传递给该类。问题是我试图在 AM 和 FM 之间进行选择,但每次运行它时,它只显示 AM 电台。我不明白为什么它会自动设置到那个电台。
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;
}
}
【问题讨论】:
-
良好的 SSCCE。您从选择 FM 开始,无法更改它。问题可能出在您的
getCurrentStaion()方法实现中。此外,对于多态性,您不需要检查if。 -
@SotiriosDelimanolis 确实如此,但 OP 说在获取它时,你会得到 AM 收音机。这引出了一个问题,OP 真的是指所说的话吗,这就是所有代码吗?
-
我怀疑问题出在您的 FMRadio 类中的 GetCurrentStation 上。可以发一下吗?
-
@Renan 我的印象是
getCurrentStaion为两个无线电返回相同的值。 -
最好把 Radio、FM&AM&XMRadio 课程的内容也贴出来。但是根据您当前的代码,我猜您的代码中存在错误-如果所有 FM&AM&XM 都从 Radio 扩展,那么您是否正确覆盖了 .getCurrentStation(),我在您的代码中看到了拼写错误( getCurrentStation() 与 getCurrentStaion() )