连接两个班级

时间:2018-11-02 09:16:16

标签: java class methods

我坚持进行这一练习,在该练习中,我应该创建一个第二类,即Car,该类与Vehicle链接。这是它的外观:

https://gyazo.com/4c0c5c29fb7dec32080f3ce59383c320

车辆类和testprogram一起很好地工作,但是现在我想将Car类连接到车辆,然后为car创建一个测试类。这是我的车辆类别:

public class Vehicle {
int speed;

// Constructor
public Vehicle() {
    this.speed = 0;

}

public class Car extends Vehicle {
    String regnr;

    public Car(String regnr) {
        this.regnr = regnr;
    }

    public String getRegNbr() {
        return this.regnr;
    }

}

public void increaseSpeed(int differenceInc) {
    int currentSpeed = this.speed;
    // Kör loopen så länge den nuvarande hastigheten inte är lika med den önskade
    while (this.speed != (currentSpeed + differenceInc)) {
        this.speed += 1;

        System.out.println("The current speed is increasing to: " + this.speed);
    }

}

public void decreaseSpeed(int differenceDec) {
    int currentSpeed = this.speed;

    while (this.speed != (currentSpeed - differenceDec)) {
        this.speed -= 1;
        System.out.println("The current speed is decreasing to: " + this.speed);
    }

}

public void brake() {
    int currentSpeed = this.speed;
    while (this.speed != 0) {
        this.speed /= 2;
        System.out.println("The current speed is decreasing to: " + this.speed);
    }
}

public int getSpeed() {
    return this.speed;
}

public void testVehicle() {

    Scanner myScanner = new Scanner(System.in);
    while (this.speed != 0) {
        System.out.println("You're driving at: " + " " + this.speed + "KM/H" + "\n\nDo you want to:"
                + "\n(1) Slow down to " + "lower speed??" + "\n(2) Maintain current speed?"
                + "\n(3) Hit the brakes?" + "\n(4) Accelerate even more?");

        int choice = myScanner.nextInt();

        if (choice == 1) {
            System.out.print("By how much would you like to decrease your speed? ");
            Scanner in = new Scanner(System.in);
            int dec = in.nextInt();
            this.decreaseSpeed(dec);
        } else if (choice == 2) {
            System.out.println("Maintaining current speed");
        } else if (choice == 3) {
            System.out.println("Breaking!");
            this.brake();
        }

        else if (choice == 4) {
            System.out.print("By how much would you like to increase your speed? (km/h)");
            Scanner in = new Scanner(System.in);
            int inc = in.nextInt();
            this.increaseSpeed(inc);
        }

        else {
            System.err.println("Incorrect value entererd.");
            System.exit(0);
        }
    }

    if (this.getSpeed() == 0)

    {
        System.out.println("Bilen står still");
    }

}

}

如您所见,testVehicle()类以及一个名为VehicleTest的小型单独测试程序将运行我创建的Vehicle类。我已将Car类添加到程序中,并且它按需扩展了车辆。我唯一的问题是如何将其实现到测试类中?

我当前的单独测试程序如下:

public class VehicleTest {

/**
 * @param args
 */
public static void main(String[] args) {
    Vehicle bmw = new Vehicle();
    Scanner scan = new Scanner(System.in);
    System.out.println("How fast do you want to drive?");

    int fast = scan.nextInt();
    bmw.increaseSpeed(fast);
    bmw.testVehicle();
}

}

2 个答案:

答案 0 :(得分:1)

您应该从车辆扩展汽车。试试这个:

public class Car1 extends Vehicle {
...
}

因此,您可以像汽车一样使用汽车。

一些信息可以阅读https://books.trinket.io/thinkjava2/chapter14.html

答案 1 :(得分:0)

从Vehicle类中派生Car1类

 public class Car1 extends Vehicle{

    String regnr;

    public Car1(String regnr) {
        super();//pass data to parent constructor if needed
        this.regnr = regnr;
    }

    public String getRegNbr() {
        return regnr;
    }

}

现在,您将可以从Vehicle类中调用公共方法。

Car1 car = new Car1("DHM1234");
car.increaseSpeed(5); // The current speed is increasing to: 5

如果要更改汽车(父类)类的任何公共/受保护方法的行为,以替换其汽车类,请覆盖该方法 即

@Override
public void brake() {
    int currentSpeed = 0;
    System.out.println("Hard break");
}

现在是否可以从汽车对象中调用brake()

car.brake(); // Hard break will be printed

请给Java Inheritance阅读以了解更多信息。