gpt4 book ai didi

java - 如何在java中的抽象类中实现接口(interface)并扩展线程

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:56 25 4
gpt4 key购买 nike

我正在努力满足作业的要求...“至少包含一个接口(interface),该接口(interface)包含至少一个实现类必须实现的方法。”

当我尝试实例化接口(interface)时,它说无法实例化接口(interface)。我不确定我做错了什么。

我尝试了多种方法来使其发挥作用。

//main class
public class Main {

/**
* @param args
*/
public static void main(String[] args) {

Ford mustang = new Ford("Mustang", 135, 125);
Chevrolet camero = new Chevrolet("Camero", 202, 100);
Dodge challenger = new Dodge("Challenger", 203, 75);

Nitrous nitro = new Nitrous();//problem code

mustang.start();
camero.start();
challenger.start();
}

}

//Abstract class
public abstract class Vehicle extends Thread implements Nitrous {

private String model;
private int speed;
private int boost;

public Vehicle(String model, int speed, int boost) {
this.model = model;
this.speed = speed;
this.boost = boost;
}

public String getmodel() {
return model;
}

public void setmodel(String model) {
this.model = model;
}

public int getspeed() {
return speed;
}

public void setspeed(int speed) {
this.speed = speed;
}

public int getboost() {
return boost;
}

public void setboost(int boost) {
this.boost = boost;
}


@Override
public void run() {
try {
go();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void go() throws InterruptedException {
int trackLength = 5000;
int checkPointPassed = 0;
for (int i = 0; i < trackLength; i += (speed + boost)) {
if (checkPointPassed * 1000 < i) {
checkPointPassed++;
System.out.println("The " + this.model + " has passed check point " + checkPointPassed);
// System.out.println(nos);
Thread.sleep(10);
}
}
}

}

//subclass one of three
public class Ford extends Vehicle {

public Ford (String model, int speed, int boost) {

super(model, speed, boost);

}

@Override
public void nos() {
// TODO Auto-generated method stub
System.out.println("The cars have Nitro!");
}

}

public class Chevrolet extends Vehicle{

public Chevrolet(String model, int speed, int boost) {
// TODO Auto-generated constructor stub.
super(model, speed, boost);
}

@Override
public void nos() {
// TODO Auto-generated method stub
System.out.println("The cars have Nitro!");
}

}

public class Dodge extends Vehicle{

public Dodge(String model, int speed, int boost) {
// TODO Auto-generated constructor stub
super(model, speed, boost);
}

@Override
public void nos() {
// TODO Auto-generated method stub
System.out.println("The cars have Nitro!");
}

}

//Interface

public interface Nitrous {

public void nos();
}

这是一场由三辆具有氮气增压功能的车辆组成的比赛。我选择使用 Nitrous 作为界面。您可以在我的代码中看到我尝试了不同的方法来使其工作,但没有成功。我什至不知道我是否已经接近或远离如何做到这一点。

最佳答案

接口(interface)仅提供实现它的特定类可以做什么的蓝图。为了给您一个具体的例子,请考虑接口(interface) Fruit

public interface Fruit {

void eat();

}

现在,实例化接口(interface)没有意义,因为它只是一个抽象。然而,实际创建 BananaApple 类的对象是有意义的。而且因为这些都是水果,所以你可以吃它们。

for (Fruit f : fruits) {
f.eat();
}

由于您的 Vehicle 类实现了 Nitrous,因此它的所有子类也可以分配给该特定类型,并且它们履行其约定,即 nos()

Ford mustang = new Ford("Mustang", 135, 125);

Nitrous asNitrous = mustang; // works
mustang.nos(); // works

相关:What is the definition of an interface in object oriented programming?

关于java - 如何在java中的抽象类中实现接口(interface)并扩展线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58456721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com