Java 子类需要有构造函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22470316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:56:53  来源:igfitidea点击:

Does a subclass NEED to have a constructor?

javasubclasssuperclass

提问by zaynv

I've been learning about inheritance and I was just curious. I know that the subclass will automatically call the superclass's constructor even if you don't use the super()operator, so I wanted to know if it is even necessary for a subclass to have a constructor in it.

我一直在学习继承,我只是好奇。我知道即使您不使用super()运算符,子类也会自动调用超类的构造函数,所以我想知道子类是否有必要在其中包含构造函数。

采纳答案by Ted Hopp

A subclass needs a constructor if the superclass does not have a default constructor (or has one that is not accessible to the subclass). If the subclass has no constructor at all, the compiler will automatically create a publicconstructor that simply calls through to the default constructor of the superclass.

如果超类没有默认构造函数(或子类无法访问的构造函数),则子类需要一个构造函数。如果子类根本没有构造函数,编译器将自动创建一个public构造函数,该构造函数简单地调用超类的默认构造函数。

Regarding calling super(): the first thing every constructor must do is either call a different constructor in the same class by calling this()(possibly with some arguments) or call a constructor of its superclass by calling super()(again, possibly with arguments). Neither of those calls can go anywhere else. If a constructor doesn't start with either one, the compiler will automatically insert a call to super()(without any arguments). So if the behavior you want is to call through to the default superclass constructor (and, many times, it is) then you don't need to explicitly call super()yourself.

关于调用super():每个构造函数必须做的第一件事是通过调用this()(可能带有一些参数)调用同一类中的不同构造函数,或者通过调用super()(同样可能带有参数)调用其超类的构造函数。这些电话都不能转到其他任何地方。如果构造函数不以任何一个开头,编译器将自动插入对super()(不带任何参数)的调用。因此,如果您想要的行为是调用默认超类构造函数(并且很多时候确实如此),那么您不需要显式调用super()自己。

There's also one situation where you do not need to provide a constructor (in fact, you can't provide one) even when the superclass has no default constructor. This case (described in section 15.9.5.1 of the Java Language Sepcification) is when you create an anonymous subclass of a class using a non-default constructor; the compiler will automatically create a constructor with the correct arguments and call up to the corresponding (non-default) superclass constructor. For instance:

还有一种情况,即使超类没有默认构造函数,您也不需要提供构造函数(实际上,您无法提供)。这种情况(在Java Language Sepcification 的 15.9.5.1 节中描述)是当您使用非默认构造函数创建类的匿名子类时;编译器将自动创建一个带有正确参数的构造函数,并调用相应的(非默认)超类构造函数。例如:

class X {
    public X(int value) { ... } // no default constructor!
    public void foo() { ... }
}

X myX = new X(3) {
    @Override
    public void foo() { ... }
};

Then myXwill be an instance of an anonymous subclass of X with a compiler-generated constructor that takes an intargument and calls super(intArg).

然后myX将是 X 的匿名子类的实例,带有编译器生成的构造函数,该构造函数接受一个int参数并调用super(intArg).

Because you cannotwrite a constructor for anonymous classes, there's an issue: what if you need to do some object initialization when the object is created? The solution is to use an instance initializer block. For example:

因为不能为匿名类编写构造函数,所以存在一个问题:如果在创建对象时需要进行一些对象初始化怎么办?解决方案是使用实例初始化块。例如:

X myX = new X(3) {
    // Field unique to this subclass of X:
    private int baz;
    {
        // code here runs as if it were at the start of every constructor
        baz = ...;
    }
    @Override
    public void foo() { ... }
};

回答by Warren Dew

If the default superclass constructor accessed through super() is available, then the subclass does not need to have an explicit constructor; it will automatically get a default no argument constructor. If the superclass has explicit constructors all of which take arguments, then the subclass needs an explicit constructor as well, since without such a constructor there would be no way to know what arguments the superclass constructor should be called with.

如果通过 super() 访问的默认超类构造函数可用,则子类不需要显式构造函数;它将自动获得一个默认的无参数构造函数。如果超类具有显式构造函数,所有这些构造函数都接受参数,那么子类也需要一个显式构造函数,因为没有这样的构造函数,就无法知道应该使用哪些参数调用超类构造函数。

回答by Bohemian

If the super class does not have a default (ie no args) constructor, then you mustdefine a constructor that calls a specific super constructor.

如果超类没有默认(即无参数)构造函数,那么您必须定义一个调用特定超构造函数的构造函数。

If the super class doeshave a default constructor, you don't have to declare a constructor, because the following constructor will be implicitlydefined for you if you don't declare any constructors:

如果超类确实有默认构造函数,则不必声明构造函数,因为如果不声明任何构造函数,将为您隐式定义以下构造函数:

SubClass() {
    super();  // Note: the no-args super constructor may itself be implicit
}

So in that case, you don't have to declare a constructor in the sub class.

因此,在这种情况下,您不必在子类中声明构造函数。

回答by Subhrajyoti Majumder

If default constructor is available in super class then compiler will include default constructor invocation in sub class's default constructor. For Example -

如果超类中有默认构造函数,则编译器将在子类的默认构造函数中包含默认构造函数调用。例如 -

class  Base(){
  Base(){ // default constructor
    ...
  }
}

class Sub extends Base{
   // no constructor
}

In this case not required.

在这种情况下不需要。

class  Base(){
  Base( int i){ // no default constructor
    ...
  }
}

class Sub extends Base{
   // no constructor
}

In this case it required.

在这种情况下,它需要。

class Sub extends Base{
   Sub(){ 
      Base(1);
   }
}