java - 隐式 super 构造函数 Shape2D() 未定义。关于 "include Java.awts.Color"

标签 java implicit explicit

我正在开发一个项目,该项目给出了此错误“隐式 super 构造函数 Shape2D 未定义。必须显式调用另一个构造函数”并且不太理解。

这是我的形状类

import java.awt.Color;
import java.lang.Comparable;

abstract class Shape implements Comparable<Shape>{
    private final int id;
    private final String name;
    private final String description;
    private Color color;
    //abstract method
    abstract double area();
    abstract double perimeter();

    //get and set and non-abstract method
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
        }

    //non default constructor
    public Shape(int id, String name, String description, Color color) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.color = color;

    }
    @Override
    public int compareTo(Shape o) {
        return 0;
    }

    @Override
    public String toString() {
        return String.format("%s,%s,%s,%s",id,name,description,color);      
    }


}

这是我的 Shape2D 类,它将给出宽度和高度变量

import java.awt.Color;// this is where the problem occur. if i remove it, the abstract class has an error " Implicit super constructor Shape() is undefined for default constructor. Must define an explicit 
 constructor and Implicit super constructor Shape() is undefined. Must explicitly invoke another constructor"

abstract class Shape2D extends Shape {
    public final double height;
    public final double width;

    Shape2D(height , width){
        this.height = height; 
        this.width = width;

    }

    public Shape2D(int id, String name, String description, Color color) {
        super(id, name, description, color);
    }
}

我有一个 super 类

最佳答案

当您在 Shape2D 中扩展 Shape 类时,如果您未指定,Java 会从 Shap2D 构造函数隐式调用父类(super class)的构造函数。因此,目前上面的代码如下所示,

Shape2D(height , width){
        super();   //this line will automatically added, when code complies.
        this.height = height; 
        this.width = width;

    }

因为在你的父类Shape中你没有任何args构造函数。它不会找到任何合适的构造函数,并且会给出您提到的错误。为了避免这种情况,您可以明确提及要调用哪个父类(super class)的构造函数。因此,代码如下所示:

Shape2D(height , width){
            super(1,"triangle","test",Color.yellow);   //Called parent class's constructor.
            this.height = height; 
            this.width = width;

        }

您已在其他构造函数中使用了 super(id, name, description, color);,但始终可以使用 Shape2D(height , width) 创建一个新实例> 构造函数。在这种情况下,Java 需要确保调用父类构造函数。

关于java - 隐式 super 构造函数 Shape2D() 未定义。关于 "include Java.awts.Color",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58785692/

相关文章:

events - C++ cli接口(interface)事件显式实现

java - 为什么我的 JFrame 图像没有更改图标?

java - 如何使用 hibernate 4.1 获取唯一结果集

java - 我需要在 3 维空间中绘制一个简单的动画箭头 vector

scala - Slick 3.1.1 模糊隐式值涉及过滤器和隐式 CanBeQueryCondition

Scala - 为什么在隐式列表之前声明空参数列表

c++ - 对 C++ 的哪些更改使复制初始化适用于具有显式构造函数的类?

java - 根据其中存在的组件调整 jpanel 的大小

scala - 使用 TraversableLike 定义的类型类不适用于 List

C++ 隐式和显式继承构造函数调用