我知道其他人也遇到了与我非常相似的问题,我尝试将这些答案应用到我的代码中,但它仍然不起作用,所以我希望你们中的一个人能够看看我的代码并解释我哪里出错了......
这是我的代码:
public class Square extends Rectangle{
String Colour;
public Square (int x, int y, int h, int w, String Co){
super (x,y,h,w);
Colour=Co;
System.out.println("Constructing a Square now");
}
public void showColour(){
System.out.println("The colour of the square is " + Colour);
}
}
第二部分:
public class InheritProgram {
public static void main (String [] args){
Square One= new Square (10,20, 15, 15, "blue");
Square colour =new Square();
colour.showColour();
//GeometricShape center= new displayCenter();
}
}
这是我收到的错误:
C:\Users\Karen\Documents\Java\Lab8-1\InheritProgram.java:5: error: constructor Square in class Square cannot be applied to given types;
Square colour =new Square();
^
required: int,int,int,int,String
found: no arguments
reason: actual and formal argument lists differ in length
1 error
Tool completed with exit code 1
任何帮助将非常感激
最佳答案
在这一行中:
Square colour =new Square();
...您正在尝试为 Square
调用一个无参数构造函数 - 但您尚未声明一个。您只声明了带有 5 个参数的构造函数,因此您必须使用它来创建新实例。
目前还不清楚为什么要创建第二个实例 - 为什么不在 One
上调用 showColour
?
(顺便说一句,我强烈敦促您开始遵循 Java 命名约定,并将您的字段设为私有(private)。如果您的缩进与问题中的缩进相匹配,也请修复它 - 它'将使您的代码更易于阅读。大多数 IDE 允许您非常轻松地格式化代码。)
关于java - 使用java的笛卡尔平面,找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17661499/