重写方法和线程
我对这个问题有点疑问,我被要求完成以下课程。
public class UpDownTrack extends OpenTrack
{
//allowedDirection attribute required
private TrainDirection allowedDir;
//Add a constructor to set the initial state to down
public UpDownTrack()
{
allowedDir = DOWN;
}
//Override the useTrack method so that only one train at a time
//can use the track
public synchronized void useTrack(TrainDirection trainDir, int id)
{
try
{
enterTrack(trainDir, id);
traverse();
exitTrack(trainDir,id);
}
catch(Exception e)
{
System.out.println("Error" + e);
}
}
// Override the enterTrack method so,a train going in the opposite
// direction is only allowed to enter the track
//Then display that a train has entered the track
public synchronized void enterTrack(TrainDirection trainDir, int id)
{
if(allowedDir != trainDir)
{
try
{
System.out.println("Train " + id + " entered track, going " + trainDir);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
//Override the exitTrack method so that the direction is changed
//before the track is exited
//Then display that a train has exited the track
public synchronized void exitTrack(TrainDirection trainDir, int id)
{
System.out.println(" Train " + id + " left track, going " + trainDir);
trainDir = (trainDir == DOWN)? DOWN : UP;
notifyAll();
}
}
从 OpenTrack 类覆盖的方法
public void useTrack(TrainDirection trainDir, int id)
{
enterTrack(trainDir, id);
traverse();
exitTrack(trainDir,id);
}
//This method doesn't currently check if it is safe to traverse the track.
//It just reports that a train has entered the track.
void enterTrack(TrainDirection trainDir, int id)
{
System.out.println("Train " + id + " entered track, going " + trainDir);
}
//This method currently just reports that a train has left the track
void exitTrack(TrainDirection trainDir, int id)
{
System.out.println(" Train " + id + " left track, going " + trainDir);
}
}
问题是我得到了错误的输出,我应该得到
Train 1 enters track going up
Train 1 leaves track
Train 3 enters track going down
Train 3 leaves track
Train 2 enters track going up
等等,但我现在得到的是序列和混乱的输出
Iam having a bit of a problem with this question I have been asked to complete the following class.
public class UpDownTrack extends OpenTrack
{
//allowedDirection attribute required
private TrainDirection allowedDir;
//Add a constructor to set the initial state to down
public UpDownTrack()
{
allowedDir = DOWN;
}
//Override the useTrack method so that only one train at a time
//can use the track
public synchronized void useTrack(TrainDirection trainDir, int id)
{
try
{
enterTrack(trainDir, id);
traverse();
exitTrack(trainDir,id);
}
catch(Exception e)
{
System.out.println("Error" + e);
}
}
// Override the enterTrack method so,a train going in the opposite
// direction is only allowed to enter the track
//Then display that a train has entered the track
public synchronized void enterTrack(TrainDirection trainDir, int id)
{
if(allowedDir != trainDir)
{
try
{
System.out.println("Train " + id + " entered track, going " + trainDir);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
//Override the exitTrack method so that the direction is changed
//before the track is exited
//Then display that a train has exited the track
public synchronized void exitTrack(TrainDirection trainDir, int id)
{
System.out.println(" Train " + id + " left track, going " + trainDir);
trainDir = (trainDir == DOWN)? DOWN : UP;
notifyAll();
}
}
Methods to overide from the OpenTrack class
public void useTrack(TrainDirection trainDir, int id)
{
enterTrack(trainDir, id);
traverse();
exitTrack(trainDir,id);
}
//This method doesn't currently check if it is safe to traverse the track.
//It just reports that a train has entered the track.
void enterTrack(TrainDirection trainDir, int id)
{
System.out.println("Train " + id + " entered track, going " + trainDir);
}
//This method currently just reports that a train has left the track
void exitTrack(TrainDirection trainDir, int id)
{
System.out.println(" Train " + id + " left track, going " + trainDir);
}
}
Problem is im getting the wrong output, i should get
Train 1 enters track going up
Train 1 leaves track
Train 3 enters track going down
Train 3 leaves track
Train 2 enters track going up
and so on, but im getting now sequence and jumbled up output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TrainDirection
是您的一个类,当您将其实例连接到字符串时(例如:System.out.println(" Train " + id + " left track, moving " + trainDir);
),它调用类的toString
方法。但从Object
继承的默认toString()
返回对象哈希码,而不是实际方向。因此,您需要重写类TrainDirection
中的public String toString()
方法,以便它将返回有用的信息给您(例如火车的方向... )TrainDirection
is a class of yours and when you concatenate an instance of it to a string (such as here:System.out.println(" Train " + id + " left track, going " + trainDir);
), it calls thetoString
method of the class. But the defaulttoString()
, which is inherited fromObject
returns the object hashcode and not the actual direction. So you need to override thepublic String toString()
method in the classTrainDirection
so it will return a useful information to you (such as the direction of the train...)