问题描述
我在尝试弄清楚如何比较AM FM和XM之间的selectedRadio时遇到问题,以便我可以确定它当前在哪一个然后返回特定无线电的电台。我知道我需要equals方法我只是不确定正确的方法来使用它来获得我正在寻找的结果。
I'm have problems trying to figure out how to compare selectedRadio between AM FM AND XM so that I can determine which one it is currently on and then return the station of the particular radio. I know that i need the equals method i'm just not sure the correct way to use it to get the result i'm looking for.
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;
}
}
推荐答案
要点:(从帖子底部移动)
您需要了解测试之间的区别 ObjA = = ObjB
, ObjA.equals(ObjB)
和 ObjA instanceof ClassB
。
You'll want to learn the difference between the tests ObjA == ObjB
, ObjA.equals(ObjB)
, and ObjA instanceof ClassB
.
此方法
public double getCurrentStation()
{
if (selectedRadio == radioAM)
{
return radioAM.getCurrentStaion();
}
else if (selectedRadio == radioFM)
{
return radioFM.getCurrentStaion();
}
return 0.0;
}
可能无法按预期工作。如果你在Radio基类(你做过)中实现 double getCurrentStation()
方法,你可以这样做:
Probably won't work as you expect. If you implement the double getCurrentStation()
method inside the Radio base class (which you did), you can just do something like:
public double getCurrentStation()
{
return selectedRadio.getCurrentStation();
}
但这可能有点过头了,所以你应该只更换 getCurrentStation()
调用 AutoRadioSystem
, selectedRadio.getCurrentStation()
。
But that's probably overkill, so you should just replace the getCurrentStation()
calls in AutoRadioSystem
with selectedRadio.getCurrentStation()
.
同样,
public void selectRadio()
{
if (selectedRadio.equals(radioAM))
selectedRadio = radioFM;
}
无法按预期工作。您可能需要类似
Won't work as you expect. You probably want something like
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
返回 false
如果 o
是 null
。
还有其他问题。
这篇关于麻烦骑马并在java中使用equals方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!