【问题标题】:How to avoid an 'Interface Abstract class error'?如何避免“接口抽象类错误”?
【发布时间】:2019-01-20 00:51:42
【问题描述】:

这是我的主要课程,我不断收到错误提示

抽象类没有被覆盖

我尝试过使汽车类抽象而不是覆盖,我尝试过覆盖和使用抽象类,但没有成功。我只是不知道我做错了什么。

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());
        } 

}

【问题讨论】:

  • 你需要学会格式化你的代码,即缩进代码以显示代码结构,大大提高我们人类代码的可读性。
  • 这段代码有很多问题,以至于如果不重写所有代码就很难知道如何提供帮助。也许您可以通过删除与您遇到的错误无关的所有行来减少示例,这样您就可以一次专注于一件事?然后非常具体地说明您遇到的错误及其发生的位置。
  • 除了@Andreas 建议格式化您的代码之外,还有一个非常重要的有助于提高可读性的约定是使用UpperCase 作为类型名称(类、接口等),使用lowerCase 作为变量和字段名称.
  • 看起来有些编译错误是由于您将代码从某处复制到某处时弄乱了换行符。字符串文字中间有换行符!请检查您的代码(在问题中)并更正这些和其他格式问题,然后再要求我们帮助您。

标签: java interface implementation


【解决方案1】:

Car 是一个抽象类,因此您不能创建它的实例。您可能应该创建另一个扩展 Car 类的类。有关信息,请参阅此答案:

https://stackoverflow.com/a/30317092/7260643

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多