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

时间:2013-10-10 04:53:04

标签: java abstraction delegation karel

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

2 个答案:

答案 0 :(得分: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();
            }    
        }

答案 1 :(得分:-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();
    }
}