构造函数覆盖可能吗? [英] Is Constructor Overriding Possible?
问题描述
我所知道的是,编译器在字节码中编写了一个默认的无参数构造函数.但是如果我们自己编写它,该构造函数将被自动调用.这种现象是构造函数覆盖吗?
What i know is, the compiler writes a default no argument constructor in the byte code. But if we write it ourselves, that constructor is called automatically. Is this phenomena a constructor overriding?
推荐答案
您所描述的内容并不重要.如果不指定默认构造函数,则编译器将创建一个默认构造函数.如果它是一个子类,它将调用默认的父构造函数(super()),它还将所有实例变量初始化为由类型的默认值确定的默认值(0 表示数字类型,false 表示布尔值,或 null用于对象).
What you describe isn't overriding. If you don't specify a default constructor, the compiler will create a default constructor. If it's a subclass, it will call the default parent constructor(super()), it will also initialize all instance variables to a default value determined by the type's default value(0 for numeric types, false for booleans, or null for objects).
当子类具有相同的名称、参数数量/类型以及与超类的实例方法相同的返回类型.在这种情况下,子类将覆盖超类的方法.有关在此处进行覆盖的信息.
Overriding happens when a subclass has the same name, number/type of parameters, and the same return type as an instance method of the superclass. In this case, the subclass will override the superclass's method. Information on overriding here.
这篇关于构造函数覆盖可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!