重写方法和线程

发布于 2024-11-06 20:36:59 字数 2557 浏览 0 评论 0原文

我对这个问题有点疑问,我被要求完成以下课程。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-11-13 20:37:00

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 the toString method of the class. But the default toString(), which is inherited from Object returns the object hashcode and not the actual direction. So you need to override the public String toString() method in the class TrainDirection so it will return a useful information to you (such as the direction of the train...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文