java - 不能应用于给定类型;java 实际参数列表和形式参数列表的长度不同
问题描述
我正在尝试在 java 中实现继承,但我不断收到错误消息:
HelloWorld.java:27:错误:类 HelloWorld.BoxShapes 中的构造函数 BoxShapes 不能应用于给定类型;Square(int side1){ ^ required: int,int found: no arguments
下面是我试图运行的代码:
public class HelloWorld{
public static void main(String []args){
}
class BoxShapes{
int side1, side2;
BoxShapes(int side1, int side2){
this.side1 = side1;
this.side2 = side2;
}
void PrintInfo(){
System.out.println("The length of side 1 is " + this.side1 +
" and the lenght of side2 is " + this.side2);
}
}
class Square extends BoxShapes{
Square(int side1){
this.side1 = side1;
}
}
class Rectangle extends BoxShapes{
Rectangle(int side1, int side2){
this.side1 = side1;
this.side2 = side2;
}
}
void compuateArea(){
int area;
area = this.side1 * this.side2;
System.out.print("Area of rectanlge is " + area);
}
void compuatePerimeter(){
int perimeter;
perimeter = 2 * (this.side1 + this.side2);
System.out.println(perimeter);
}
//@override
void printInfo(){
super.printInfo();
}
}
我对错误消息的理解是构造函数BoxShapes
需要 2 个整数。所以我会理解 Square 类失败,但 rectangle 类会抛出与上面相同的错误:
> HelloWorld.java:32: error: constructor BoxShapes in class HelloWorld.BoxShapes cannot be applied to given types;
> Rectangle(int side1, int side2){
> ^
> required: int,int
> found: no arguments
所以我的理解是不正确的。
解决方案
推荐阅读
- html - 在html中使用overflow-x时无法显示自动完成的地方
- node.js - 如何让我的应用记住页面刷新时的当前路由(使用 react-router 和 express)
- unity3d - Unity3D:创建脚本以爬梯子
- c++ - 当 RVO 可以应用时,为什么按 shared_ptr 而不是按值返回?
- vue.js - 为什么我的 vue 组件没有出现在 vue-devtools 中并呈现为 data-v-xxx div?
- python - NameError——作为内部类的 Python-Enums 可以互相看不到吗?
- html - 如何按按钮进行操作
- javascript - 添加反应原生矢量图标以使用反应原生网络反应原生时出错
- javascript - 滑块上带有值的输入类型范围
- android - 如何从 Android Viewmodel 中的线程返回结果?