Java 如何避免';接口抽象类错误';?
这是我的主要课程,我经常会犯这样的错误 抽象类尚未被重写 我尝试过使car类抽象而不是重写,我尝试过重写并使用抽象类,但没有成功。我只是不知道我做错了什么Java 如何避免';接口抽象类错误';?,java,interface,implementation,Java,Interface,Implementation,这是我的主要课程,我经常会犯这样的错误 抽象类尚未被重写 我尝试过使car类抽象而不是重写,我尝试过重写并使用抽象类,但没有成功。我只是不知道我做错了什么 public abstract class Car implements CarbonFootprint { private double Car; private double MilesDrivenPerYear; private double MilesPerGallon; //Construct
public abstract class Car implements CarbonFootprint {
private double Car;
private double MilesDrivenPerYear;
private double MilesPerGallon;
//Constructor
public Car(double MilesDrivenPerYear, double MilesPerGallon) {
this.MilesDrivenPerYear = MilesDrivenPerYear;
this.MilesPerGallon = MilesPerGallon;
}
//Return miles driven per year
public double getMilesDrivenPerYear() { return MilesDrivenPerYear; }
//Return Miles per Gallon
public double getMilesPerGallon() { return MilesPerGallon; }
public void setMilesDrivenPerYear(double MilesDrivenPerYear) {
this.MilesDrivenPerYear = MilesDrivenPerYear;
}
public void setMilesPerGallon(double MilesPerGallon) {
this.MilesPerGallon = MilesPerGallon;
}
@Override
public String toString() {
return String.format("%s: %n%s: %s", "Car", "Miles
Driven: ",getMilesDrivenPerYear(), "Miles per
Gallon; ",getMilesPerGallon());
}
public abstract double Car();
public double getCarbonFootprint() {
return Car = getMilesDrivenPerYear() / getMilesPerGallon() * 19.82;
}
}
//end car class'
public class CarbonFootprintTest {
public static void main(String[] args) {
ArrayList FootprintList = new ArrayList();
Car Footprint1 = new Car(25, 36);
FootprintList.add(Footprint1);
Building Footprint2 = new Building(78, 78);
FootprintList.add(Footprint2);
Bicycle Footprint3 = new Bicycle(90);
FootprintList.add(Footprint3);
System.out.println("Shaina Carbon Footprint Calculator");
for (Object Footprint: FootprintList) {
System.out.printf("Miles Driven:");
System.out.printf("Car Carbon Footprint",
Footprint2.getCarbonFootprint());
}
}
Car是一个抽象类,因此不能创建它的实例。您可能应该创建另一个类来扩展Car类。有关信息,请参见此答案:
您需要学习设置代码格式,即缩进代码以显示代码结构,这大大提高了代码的可读性。这段代码存在很多问题,如果不重写所有代码,就很难知道如何提供帮助。也许你可以通过删除所有与错误无关的行来简化这个例子,这样你就可以一次只关注一件事了?除了@Andreas建议对代码进行格式化外,还有一个非常重要的帮助可读性的约定,就是对类型名(类、接口等)使用
大写字母
变量名和字段名的lowerCase
。看起来有些编译错误是由于在将代码从某个地方复制到另一个地方时弄乱了换行符造成的。字符串文字中间有断线!请检查您的代码(在问题中)并更正这些和其他格式问题,然后再请求我们帮助您。