简体   繁体   English

子类构造函数隐式调用默认构造函数

[英]Implicit calling of default constructor by subclass constructor

The following code gives an error when I try to create an object of type B. My question is why isn't the default constructor of A called? 当我尝试创建类型为B的对象时,以下代码给出了一个错误。我的问题是为什么不调用A的默认构造函数?

class A
{
  private int a;

  A(int a)
  {
    this.a = a;
    System.out.println("This is constructor of class A");
  }
} 

class B extends A
{
  private int b;
  private double c;

  B(int b, double c)
  {
    this.b = b;
    this.c = c;
    System.out.println("This is constructor of class B");
  } 
} 

My question is why isn't the default contructor of A called? 我的问题是为什么不调用A的默认构造函数?

Because there isn't one. 因为没有一个。 When you provide your own parameterized constructor, the compiler won't add the default constructor. 当您提供自己的参数化构造函数时,编译器不会添加默认构造函数。 So, the class A which you seem to be thinking have a 0-arg constructor, doesn't have any. 因此,您似乎在考虑的类A具有0-arg构造函数,没有任何构造函数。 You've to add one explicitly. 您必须显式添加一个。

The default constructor of A would imply new A(). A的默认构造函数将暗示新的A()。 You do not have that constructor available which means the only way to construct A is to call new A(int). 您没有可用的构造函数,这意味着构造A的唯一方法是调用new A(int)。 Since there is no default constructor B has to explicitly call the super constructor of A to initialize A correctly. 由于没有默认构造函数,因此B必须显式调用A的超级构造函数以正确初始化A。

因为,当Java中没有构造函数时,仅调用默认构造函数。

When you define your own constructor the default constructor is no longer automatically inserted by the java compiler. 当您定义自己的构造函数时,默认的构造函数将不再由Java编译器自动插入。

In your code when B is constructed there is an implicit call on the first line to the super class construtor. 在您的代码中,当构造B时,在第一行上会隐式调用超类构造函数。 super(); Although, since you have overridden the default constructor for A, there is no constructor call that will work since the compiler will not automatically call the super-class constructor with the necessary argument. 尽管由于您覆盖了A的默认构造函数,但是没有编译器调用会起作用,因为编译器不会自动使用必要的参数来调用超类构造函数。 You should add a line to the first line of the B constructor to call the super class constructor of A with an int argument it requires or define a constructor for A similar to the default constructor that takes no arguments. 您应该在B构造函数的第一行中添加一行,以使用所需的int参数调用A的超类构造函数,或者为A定义一个构造函数,类似于不带参数的默认构造函数。

You could overload the constructor so that one constructor is parameterless like a default constructor and then have other constructors that take parameters. 您可以重载构造函数,以便一个构造函数像默认构造函数一样无参数,然后让其他构造函数接受参数。 :D :D

An example of this is as follows, based on your code: 根据您的代码,此示例如下:

class A
{
 private int a;

 A( int a)
 {
  this.a =a;
  System.out.println("This is constructor of class A");
}

//overload constructor with parameterless constructor similar to default 
A() { 
   System.out.println("this is like a default no-args constructor");
}
 } // end class A

class B extends A
{
 private int b;
 private double c;

   // add in another constructor for B that callers can use
   B() { 
      int b = 9;
      System.out.println("new B made in parameterless constructor");

   }

   B(int b,double c)
   {
     super(b); // this calls class A's constructor that takes an int argument :D
     this.b=b;
     this.c=c;
      System.out.println("This is constructor of class B");
  } 
}  // end class B

Try adding in your extended class constructor that references super class constructor ie 尝试添加引用超类构造函数的扩展类构造函数,即

class B extends A
{
private int b;
private double c;
B(int b,double c, int superVal)
{
super(superVal);
this.b=b;
this.c=c;
System.out.println("This is constructor of class B");
} 
} 

The default constructor is a constructor which doesn't contain any arguments. 默认构造函数是不包含任何参数的构造函数。 If there is no suchdefault constructor,the compiler creates one, provided there shud not be any other parameterized constructor available. 如果没有这样的默认构造函数,则编译器将创建一个默认构造函数,前提是应该没有其他可用的参数化构造函数。

In this eg, as there is a parameterized constructor available, the compiler doesn't create a default constructor. 在此示例中,由于有可用的参数化构造函数,因此编译器不会创建默认构造函数。 And as there is no default/parameterless constructor available , it results in compilation error 而且由于没有可用的默认/无参数构造函数,因此会导致编译错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM