简体   繁体   English

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

[英]How do I call an instance in the main driver from an abstract class?

So pretty much I need my delegating class (ref) to be able to read instances of a class created in the main driver. 因此,几乎我需要我的委托类(ref)才能读取在主驱动程序中创建的类的实例。 I tried to create the class references in ref, but that only created more objects in the world and didn't make the original ones move. 我尝试在ref中创建类引用,但是这样只会在世界上创建更多对象,而不会移动原始对象。

Main driver code: 主驱动程序代码:

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 code: 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();
    }
}

EDIT: Sorry, I should have clarified, but we're not allowed to change the code in the main driver because it was already given to us, so I have to somehow make it work through only the TrackReferee class. 编辑:对不起,我应该澄清一下,但是我们不允许在主驱动程序中更改代码,因为它已经交给我们了,所以我必须以某种方式使其仅通过TrackReferee类起作用。

You need to create references of AbstractTrackRobot in your TrackReferee class. 您需要在TrackReferee类中创建AbstractTrackRobot的引用。 When you create an object of TrackReferee. 创建TrackReferee的对象时。 You will pass the AbstractTrackRobot class objects created in main. 您将传递在main中创建的AbstractTrackRobot类对象。 This way the object of TrackReferee class will have references to those objects of AbstractTrackRobot class that were created in main. 这样,TrackReferee类的对象将引用在main中创建的AbstractTrackRobot类的那些对象。 Hope your problem is clear. 希望你的问题很清楚。

   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();
        }
    }

This can be an alternate solution as per your requirement ie, without change in constructor 根据您的要求,这可以是替代解决方案,即无需更改构造函数

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();
            }    
        }

in your main class, public static void main(String args[]) 在您的主类中,公共静态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();
}

In your ref, 在您的参考中,

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM