gpt4 book ai didi

java - 如何从抽象类调用主驱动程序中的实例?

转载 作者:行者123 更新时间:2023-12-01 04:17:28 25 4
gpt4 key购买 nike

所以我非常需要我的委托(delegate)类(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 类工作。

最佳答案

您需要在 TrackReferee 类中创建 AbstractTrackRobot 的引用。当你创建TrackReferee的对象时。您将传递在 main.c 中创建的 AbstractTrackRobot 类对象。这样,TrackReferee 类的对象将引用在 main.c 中创建的 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();
}
}

关于java - 如何从抽象类调用主驱动程序中的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19287336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com