如何停止另一个方法的运行方法?

时间:2019-03-06 00:38:59

标签: java inheritance polymorphism override super

这是我的大学作业,我几乎完成了所有工作。应该使用方法重载,这是我必须在我的类SafeRidingMower(将在解释后插入代码)中完成的,该方法从我的教授的类(我不能更改任何事情)RidingMower扩展而来。

RidingMower类:

    import java.util.*;

/**
 * The riding mower class developed by the engineering department. Contains all the functionality needed
 * for a working riding mower.
 *
 */
public class RidingMower {

    private boolean rider_in_seat = false;

    private boolean is_engine_on = false;

    private boolean is_mower_enabled = false;

    private Random rn = new Random();

    /**
     * Constructor for Riding Mower which automatically launches the main menu.
     */
    public RidingMower()
    {
        mainMenu();
    }

    /** Starts the engine of the riding mower (ignition start) */
    public void start_engine()
    {
        System.out.println("<<< Starting riding mower >>>");

        is_engine_on = true;
    }

    /** Method that randomly seats and unseats a driver */
    private void sit_rider()
    {

        int random = rn.nextInt(5);

        if ( random<=1 )
             rider_in_seat = true;
        else rider_in_seat = false;       

        System.out.print("\t=>Seated? " + rider_in_seat + "\n");
    }

    /** Seat sensor. Returns true if there is a driver seated on the mower or false
     * if no driver is sitting on the mower.
     * @return 
     */
    public boolean check_seat_sensor()
    {   
        return rider_in_seat;
    }

    /** Method that engages the mower blades and begins mowing if the ignition is turned on */
    public void mow()
    {
        start_mowing();
    }

    /** Primary method that handles the mowing. Checks to see if the ignition is on then the 
      mowing starts otherwise the cutting blades are lowered and ready to mow upon engine start
      */
    private void start_mowing()
    {

        is_mower_enabled = true;

        System.out.println("Mower blade ready to mow");

        if ( !is_engine_on ) return;

        int i=1;

        while (true)
        {
            mowing(i++);

        }
    }

    /** Mower fully functioning here and blades are spinning. Simulates a rider being thrown off 
     the mower at step 50,000 */
    private void mowing(int i)
    {
        System.out.println( "Mowing..." + i);

        if ( i>50000 ) rider_in_seat = false;

        if ( rider_in_seat == false )
            System.out.println("\tOMG Mower is moving without a driver?!");

        mowing_hook();
    }

    /** Hook/Stub function called while the mower is mowing. Other departments divisions can override */
    public void mowing_hook()
    {

    }

    /** Turns off the ignition. Stops the mower if running. */
    public void stop_mower()
    {
        System.out.println("Stopping the mower.");
        System.exit(0);
    }

    /** Main menu of the mower simulation. Asks user to check/seat a driver, start the mower, 
     * begn mowing the lawn, or exiting the simulation. Method is private and can not be 
     * overriden.
     */
    private void mainMenu()
    {
        Scanner sc = new Scanner( System.in );
        int option;

        do
        {
            System.out.println();
            System.out.println("1. Is driver seated? (" + rider_in_seat +")" );
            System.out.println("2. Start mower");
            System.out.println("3. Mow the lawn");
            System.out.println("4. Stop mower (if running) and Exit.");
            System.out.println();
            System.out.println("CHOOSE 1-4");

            option = sc.nextInt();

            switch ( option )
            {        
                case 1: sit_rider();
                        break;

                case 2: start_engine();
                        if ( is_mower_enabled ) mow();
                        break;

                case 3: mow();
                        break;

                case 4: stop_mower();
                        break;
            }

        } while ( true ); // Infinite loop here. Select option #4 to exit the program.
    }
}

SafeRidingMower类:

public class SafeRidingMower extends RidingMower 
{

    @Override
    public void start_engine() {
        if (check_seat_sensor() == true)
            super.start_engine();
        else
            System.out.println("Seat sensor does not detect a driver."
                    + "Engine will not start.");
    }

    @Override
    public boolean check_seat_sensor() {
        return super.check_seat_sensor();
    }

    @Override
    public void mow() {
        if (check_seat_sensor() == true)
            super.mow();
        else if (check_seat_sensor() == false)
            System.out.println("Seat sensor does not detect a driver."
                    + " Mower will not start.");
        else
            System.out.println("Engine is not on. Cannot start mowing.");
    }

    @Override
    public void mowing_hook() {

    }
}

还有驱动程序,我认为它与我遇到的特定问题无关,但是无论如何,这里还是存在。

public class TheLawnMowerManDriver {

public static void main(String[] args)
{

    RidingMower mower = new SafeRidingMower();

}

}

我需要做的是:在RidingMower类中,有一个我不能重写的方法mowing(),但是我可以重写在方法mowing()末尾调用的方法mowing_hook()。我应该做的是在变量i = 50000时停止割草机的运行,但是我不知道该怎么做。我尝试将super关键字与变量一起使用,但是没有用(我不知道它是否应该工作,我只是在几个类中才被介绍给super关键字)。

有人知道我可以通过覆盖mowing_hook()方法来停止剪草机/停止变量i的计数吗?

2 个答案:

答案 0 :(得分:0)

一次:
    1. i是方法mowing()的局部变量,并且;
    2.方法mowing()不会将i的值传递给方法mowing_hook()

因此,无法在方法i中读取mowing_hook()的值。

我有两种方法:
    1.你的教授在骗你
    2.您误会了作业

我会提供一些替代品:

  1. 更改方法mowing_hook()

    的签名
    /** Mower fully functioning here and blades are spinning. Simulates a rider being thrown off the mower at step 50,000 */
    private void mowing(int i)
    {
        System.out.println( "Mowing..." + i);
    
        if ( i>50000 ) rider_in_seat = false;
    
        if ( rider_in_seat == false )
            System.out.println("\tOMG Mower is moving without a driver?!");
    
        mowing_hook(i);
        }
    
        public void mowing_hook(int i)
        {
    
        }
    }
    

    并以这种方式覆盖

    @Override
    public void mowing_hook(int i) {
        if (i == 50000) stop_mower();
    }
    
  2. 或使用实例变量rider_in_seat

    @Override
    public void mowing_hook(int i) {
        if (!rider_in_seat) stop_mower(); 
    }
    

    但是请注意,由于行rider_in_seat

  3. i > 50000仅在if ( i>50000 ) rider_in_seat = false;时才设置为false。

答案 1 :(得分:0)

我找到了解决这个问题的方法。

正如我所说,我相信是您误​​解了这个问题,还是您的教授在欺骗您。

无论如何,如果我们观看mowing()方法执行indirectly,就有可能停止执行。

这个想法是包装System.out并记录最后打印的字符串。因此,在mowing_hook()中,我们可以看到手表已打印并在必要时停止。

您可以在此处找到一个有效的示例:jdoodle.com/a/12KZ

总结:

将此课程添加到您的代码中

class SystemOutWrapper extends PrintStream{
    String lastLine;

    public SystemOutWrapper(OutputStream out){
        super(out);
    }

    @Override
    public void println (String str){
        this.lastLine = str;
        super.println(str);
    }

    public String getLastLine(){
        return lastLine;
    }
}

并以这种方式覆盖方法

@Override
public void mowing_hook() {

    if (! (System.out instanceof SystemOutWrapper)){
        System.setOut(new SystemOutWrapper(System.out));
    }

    //I used 100 as the online editor limitation. But you should change to 50000
    if ("Mowing...100".equals( ((SystemOutWrapper) System.out).getLastLine())){
        System.out.println("Limite reached! Should stop now!");
        stop_mower();
    }
}