Java 连接两个类
我被困在这个练习中,我应该创建一个叫做Car的第二个类,它链接到Vehicle。它应该是这样的: vehicle类和testprogram一起工作得很好,但是现在我想将类Car连接到vehicle,然后为Car创建一个testclass。这是我的车辆等级:Java 连接两个类,java,class,methods,Java,Class,Methods,我被困在这个练习中,我应该创建一个叫做Car的第二个类,它链接到Vehicle。它应该是这样的: vehicle类和testprogram一起工作得很好,但是现在我想将类Car连接到vehicle,然后为Car创建一个testclass。这是我的车辆等级: public class Vehicle { int speed; // Constructor public Vehicle() { this.speed = 0; } public class Car extends Ve
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类添加到程序中,它应该扩展vehicle。我唯一的问题是如何在测试类中实现它
我当前的单独测试程序如下所示:
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();
}
}你应该把车从车上伸出来。试试这个:
public class Car1 extends 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
如果要更改车辆(父)类的任何公共/受保护方法对车辆类的行为,请重写该方法
i、 e
现在,如果您从car对象调用brake()
car.brake(); // Hard break will be printed
阅读了解更多信息。您知道Java中的继承吗?如果没有,请检查我是否编辑了我的初始帖子,以便您可以看到我的位置。我剩下的一件事就是创建测试类,但我不知道它应该在哪里?它应该在我在底部发布的测试程序中,还是应该添加到Vehicle类中的testVehicle方法中?您可以同时执行这两个操作。你这样做也很好。好吧,让我们假设我在程序中说:你的注册号是(…),然后按现在的样子打印程序的其余部分。如果我输入Car1 car=新Car1(“DHM1234”);车辆下方宝马=新车();所有我得到的是“汽车无法解析为类型”你在你的汽车类别内做错了,你的汽车类别应该在汽车类别之外。在一个单独的文件中会更好我到底做错了什么?另外,我是否应该键入Car1 car=new Car1(“DHM1234”)并删除车辆bmw=new Vehicle(),因为car类继承了车辆类?
car.brake(); // Hard break will be printed