如何在 java 中实现接口并在抽象 class 中扩展线程
How to implement interface AND extend a thread in an abstract class in java
我正在努力完成作业的要求..."Include at least one interface that contains at least one method that implementing classes must implement."
当我尝试实例化接口时,它说无法实例化接口。我不确定我做错了什么。
我已经尝试了几种方法让它发挥作用。
//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 成为界面。您可以在我的代码中看到我尝试了不同的方法来使其工作并且 none 已经成功。我什至不知道我是否接近或远离如何做到这一点。
接口仅提供蓝图,说明实现它的特定 class 可以 做什么。给你一个具体的例子,考虑接口 Fruit
.
public interface Fruit {
void eat();
}
现在,实例化接口没有意义,因为它只是一个抽象。但是,实际创建 class Banana
或 Apple
的对象是有意义的。而且这些都是水果,你可以吃。
for (Fruit f : fruits) {
f.eat();
}
由于您的 Vehicle
class 实现了 Nitrous
,它的所有子 class 也可以分配给该特定类型,并且它们履行其合同,这是 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?
我正在努力完成作业的要求..."Include at least one interface that contains at least one method that implementing classes must implement."
当我尝试实例化接口时,它说无法实例化接口。我不确定我做错了什么。
我已经尝试了几种方法让它发挥作用。
//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 成为界面。您可以在我的代码中看到我尝试了不同的方法来使其工作并且 none 已经成功。我什至不知道我是否接近或远离如何做到这一点。
接口仅提供蓝图,说明实现它的特定 class 可以 做什么。给你一个具体的例子,考虑接口 Fruit
.
public interface Fruit {
void eat();
}
现在,实例化接口没有意义,因为它只是一个抽象。但是,实际创建 class Banana
或 Apple
的对象是有意义的。而且这些都是水果,你可以吃。
for (Fruit f : fruits) {
f.eat();
}
由于您的 Vehicle
class 实现了 Nitrous
,它的所有子 class 也可以分配给该特定类型,并且它们履行其合同,这是 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?