我有一个类状态有3种方法:
getPedestrianStatusText()
getGatesStatusText()
getDrawBridgePositionText()
(每个方法的目的是包含一个String,它是某个东西的状态,并且在按下按钮时应该改变。)
我有一个扩展Status的FileStatus类,它可以按如下方式访问这些方法:
public class FileStatus extends Status{
public FileStatus(){
}
public void writeToFile(){
String file_text = getPedestrianStatusText() + " " + getGatesStatusText() + " " + getDrawBridgePositionText();
}
}
在一个单独的类中,这三个方法被访问并按预期运行,当按下一个按钮并调用它们时,它们会随着我们的编程而改变。我的问题是,当从FileStatus类访问这些方法时,返回的String不会像从不同的类调用这些方法时那样改变。我怀疑我的遗产有错误,但我不确定是什么问题。任何建议都表示赞赏。
这是我的状态类:
public class Status{
public Status(){
}
public String getGatesStatusText(){
//returns a string with the status of the gates
if(GatesOpen){
gatesStatus="The gates are open";
}
else{
gatesStatus="The gates are closed";
}
return gatesStatus;
}
public String getDrawBridgePositionText(){
if(DrawbridgeUp){
DrawBridgeStatusText="Drawbridge is up";
}
else{
DrawBridgeStatusText="Drawbridge is down";
}
return DrawBridgeStatusText;
}
public String getPedestrianStatusText(){
return pedStatusText;
}
}
这是一个单独的类,其中调用了FileStatus:
public class MenuBar extends JMenuBar {
DrawStatus draw;
public MenuBar(DrawStatus d){
final FileStatus file = new FileStatus();
});
JMenuItem printStatus = new JMenuItem("Print Status");
printStatus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event){
file.writeToFile();
}
});
} }
最后是函数按预期运行的最后一个类:
public class DrawStatus extends JPanel{
Status status = new Status();
public DrawStatus(){
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("SYSTEM STATUS: "+status.getGatesStatusText() + " | " +status.getPedestrianStatusText() + " | " + status.warningLightStatusText() + " | "+status.getDrawBridgePositionText(), 0, getHeight()-10);
}