子类需要有构造函数吗? [英] Does a subclass NEED to have a constructor?

查看:30
本文介绍了子类需要有构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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 public constructor that simply calls through to the default constructor of the superclass.

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

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.

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

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:

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

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

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

Then myX will be an instance of an anonymous subclass of X with a compiler-generated constructor that takes an int argument and calls super(intArg).

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

Because you cannot write 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() { ... }
};

这篇关于子类需要有构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆