隐式超级构造函数 Shape2D() 未定义。关于 "include Java.awts.Color"
implicit super constructor Shape2D() is undefined. in regards to with "include Java.awts.Color"
我正在做一个给出这个错误的项目“隐式超级构造函数 Shape2D 未定义。必须显式调用另一个构造函数”并且我不太明白。
这是我的形状Class
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 class,它将给出宽度和高度变量
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);
}
}
我有一个超级class那个
当您在 Shape2D
中扩展 Shape
class 时,从 Shap2D's
构造函数 java 隐式调用超级 class构造函数,如果你不指定。因此,目前上面的代码如下所示,
Shape2D(height , width){
super(); //this line will automatically added, when code complies.
this.height = height;
this.width = width;
}
因为在你的父 class Shape
你没有任何参数构造函数。它不会找到任何合适的构造函数,并且会像您提到的那样给出错误。为避免这种情况,您可以明确提及要调用哪个超级 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 需要确保调用父 class 构造函数。
我正在做一个给出这个错误的项目“隐式超级构造函数 Shape2D 未定义。必须显式调用另一个构造函数”并且我不太明白。
这是我的形状Class
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 class,它将给出宽度和高度变量
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);
}
}
我有一个超级class那个
当您在 Shape2D
中扩展 Shape
class 时,从 Shap2D's
构造函数 java 隐式调用超级 class构造函数,如果你不指定。因此,目前上面的代码如下所示,
Shape2D(height , width){
super(); //this line will automatically added, when code complies.
this.height = height;
this.width = width;
}
因为在你的父 class Shape
你没有任何参数构造函数。它不会找到任何合适的构造函数,并且会像您提到的那样给出错误。为避免这种情况,您可以明确提及要调用哪个超级 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 需要确保调用父 class 构造函数。