1

所以几乎我需要我的委托类(ref)能够读取在主驱动程序中创建的类的实例。我试图在 ref 中创建类引用,但这只会在世界上创建更多对象,并没有使原始对象移动。

主驱动代码:

public static void main(String args[])  
{   
    AbstractTrackRobot Sprint100Bot = new Sprint100Bot(1,1,North,0);
    AbstractTrackRobot Sprint200Bot = new Sprint200Bot(1,1,North,0);
    AbstractTrackRobot Sprint400Bot = new Sprint400Bot(1,1,North,0);

    AbstractReferee ref = new TrackReferee(1,1,North,0);
    ref.meet();
}

TrackReferee代码:

public class TrackReferee extends AbstractReferee
{
    AbstractTrackRobot Sprint100Bot = new Sprint100Bot(1,1,North,0);
    AbstractTrackRobot Sprint200Bot = new Sprint200Bot(1,1,North,0);
    AbstractTrackRobot Sprint400Bot = new Sprint400Bot(1,1,North,0);

    public TrackReferee(int st, int ave, Direction dir, int Beepers)
    {
        super(st, ave, dir, Beepers);
    }

    public void meet()
    {
        Sprint100Bot.run();
        Sprint200Bot.run();
        Sprint400Bot.run();
    }
}

编辑:对不起,我应该澄清一下,但我们不允许更改主驱动程序中的代码,因为它已经提供给我们,所以我必须以某种方式使其仅通过 TrackReferee 类工作。

4

2 回答 2

1

您需要在 TrackReferee 类中创建 AbstractTrackRobot 的引用。当你创建一个 TrackReferee 的对象时。您将传递在 main 中创建的 AbstractTrackRobot 类对象。这样,TrackReferee 类的对象将具有对在 main 中创建的 AbstractTrackRobot 类的那些对象的引用。希望你的问题很清楚。

   public static void main(String args[])  
    {   
        AbstractTrackRobot Sprint100Bot = new Sprint100Bot(1,1,North,0);
        AbstractTrackRobot Sprint200Bot = new Sprint200Bot(1,1,North,0);
        AbstractTrackRobot Sprint400Bot = new Sprint400Bot(1,1,North,0);

        AbstractReferee ref = new TrackReferee(1,1,North,0, Sprint100Bot ,Sprint200Bot , Sprint400Bot);
        ref.meet();
    }


    public class TrackReferee extends AbstractReferee
    {
        public AbstractTrackRobot Sprint100Bot;
        public AbstractTrackRobot Sprint200Bot;
        public AbstractTrackRobot Sprint400Bot;

        public TrackReferee(int st, int ave, Direction dir, int Beepers, AbstractTrackRobot Sprint100Bot, AbstractTrackRobot Sprint200Bot ,  AbstractTrackRobot Sprint400Bot)
        {
            super(st, ave, dir, Beepers);
    this.Sprint100Bot = Sprint100Bot;
    this.Sprint200Bot = Sprint200Bot;
    this.Sprint400Bot = Sprint400Bot;

        }

        public void meet()
        {
            Sprint100Bot.run();
            Sprint200Bot.run();
            Sprint400Bot.run();
        }
    }

这可以根据您的要求作为替代解决方案,即无需更改构造函数

public static void main(String args[])  
{   
    AbstractTrackRobot Sprint100Bot = new Sprint100Bot(1,1,North,0);
    AbstractTrackRobot Sprint200Bot = new Sprint200Bot(1,1,North,0);
    AbstractTrackRobot Sprint400Bot = new Sprint400Bot(1,1,North,0);

    AbstractReferee ref = new TrackReferee(1,1,North,0);
    ref.Sprint100Bot = Sprint100Bot;
    ref.Sprint200Bot = Sprint200Bot;
    ref.Sprint400Bot = Sprint400Bot;
    ref.meet();
}



   public class TrackReferee extends AbstractReferee
        {
            AbstractTrackRobot Sprint100Bot;
            AbstractTrackRobot Sprint200Bot;
            AbstractTrackRobot Sprint400Bot;

            public TrackReferee(int st, int ave, Direction dir, int Beepers)
            {
                super(st, ave, dir, Beepers);


            }

            public void meet()
            {
                Sprint100Bot.run();
                Sprint200Bot.run();
                Sprint400Bot.run();
            }    
        }
于 2013-10-10T04:57:33.330 回答
-1

在您的主类中, public static void main(String args[])

{
        AbstractTrackRobot Sprint100Bot = new Sprint100Bot(1,1,North,0);
        AbstractTrackRobot Sprint200Bot = new Sprint200Bot(1,1,North,0);
        AbstractTrackRobot Sprint400Bot = new Sprint400Bot(1,1,North,0);

        AbstractReferee ref = new TrackReferee(Sprint100Bot,Sprint200Bot,Sprint400Bot );

  ref.meet();
}

在您的参考文献中,

public class TrackReferee extends AbstractReferee
{

 AbstractTrackRobot Sprint100Bot;
        AbstractTrackRobot Sprint200Bot;
        AbstractTrackRobot Sprint400Bot;

 public TrackReferee(AbstractTrackRobot Sprint100Bot, AbstractTrackRobot Sprint200Bot,  AbstractTrackRobot Sprint400Bot)
    {
       this.Sprint100Bot = Sprint100Bot;
       this.Sprint200Bot = Sprint200Bot;
       this.Sprint400Bot = Sprint400Bot;
    }
public void meet()
    {
        Sprint100Bot.run();
        Sprint200Bot.run();
        Sprint400Bot.run();
    }
}
于 2013-10-10T05:03:08.677 回答