Error: Constructor A in class A cannot be applied to given types

Error: Constructor A in class A cannot be applied to given types

public class A{

    public A(String x){
        System.out.println("A constructor Called "+x);
    }
     public static void main(String []args){
        System.out.println("Hello World");
        A a= new B("b");
     }
}

class B extends A{
    public B(String x){
        System.out.println("B constructor Called "+x);
    }
}

这个非常简单的程序有什么问题,我无法定位。 编译时出现以下错误:

A.java:13: error: constructor A in class A cannot be applied to given types;                                                                                                    
    public B(String x){                                                                                                                                                         
                      ^                                                                                                                                                         
  required: String                                                                                                                                                              
  found: no arguments                                                                                                                                                           
  reason: actual and formal argument lists differ in length  

您需要调用 super 构造函数:

public B(String x){
    super(x);
    System.out.println("B constructor Called "+x);
}

通常,有一个对默认无参数构造函数的隐式调用,但 A 没有其中之一。写入 super(x); 将调用 public A(String x) ... 构造函数,这是 A class.

中唯一可用的构造函数

由于classA没有默认构造函数,需要告诉classB如何构造它的父类:

class B extends A{
    public B(String x){
        super(x);  // this constructs the parent class
        System.out.println("B constructor Called "+x);
    }
}

错误告诉您必须调用的构造函数需要一个字符串:

required: String

... 但是您正在调用的那个(这是默认构造函数,因为您没有调用 super )没有参数:

found: no arguments

补充一下。
如果您将在 hte parent class 中编写 no-argumnet 构造函数,它将不会显示错误。因为默认情况下 super() 调用发生在构造函数内部。因此,当它调用父 class 构造函数时,它不会在父 class.

中获得无参数网络构造函数