首页 > 解决方案 > 不能应用于给定类型;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

所以我的理解是不正确的。

标签: java

解决方案


推荐阅读