Java 中的构造函数覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20905496/
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 05:12:25  来源:igfitidea点击:

Constructor Overriding in Java

javainheritanceconstructoroverriding

提问by Jagruttam

Can we override a constructor of super class in sub class of the same class. If yes, how? If no, why?

我们可以在同一个类的子类中覆盖超类的构造函数吗?如果是,如何?如果没有,为什么?

class Super{}
class Sub extends Super
{
    //write code, if yes
}

回答by Juned Ahsan

Constructors cannot be overridden. The concept of constructor is to create an instance of class, and that behavior belongs to that class only and cannot be overidden.

构造函数不能被覆盖。构造函数的概念是创建一个类的实例,该行为只属于该类,不能被覆盖。

回答by Keerthivasan

No, you cannot overridethe constructorof the Superclass. JVMwill definitely call the super class constructorwhile creating the child class instance. So, whenever you createa subclass instance, it will invoke the baseclass constructorand then continuewith the subclass constructor statements. Constructorsare notMethodsthat can be overriden

,你不能覆盖constructor中的Super类。JVM在创建子类实例时肯定会调用超类构造函数。所以,当你创建一个子类的实例,它会调用基类的构造函数,然后继续子类的构造函数语句。是不是那个可以被覆盖的ConstructorsMethods

回答by Suresh Atta

Can we override a constructor of super class in sub class of the same class?

我们可以在同一个类的子类中覆盖超类的构造函数吗?

No.

不。

With inheritance you can get/obtain instance members only. Constructor is not a member of class. Jvm treats it specially to construct an object . You can check that by seeing byte code instructions.

通过继承,您只能获取/获取实例成员。构造函数不是类的成员。JVM 特别对待它来构造一个对象。您可以通过查看字节码指令来检查。

By ovveriding, what you acheive ?? A constructor must construct the current object.

ovveriding,你有什么成就??构造函数必须构造当前对象。

回答by Tim B

You can sort of override the constructor. You cannot actually override one, but you can achieve some of the same results.

您可以覆盖构造函数。您实际上无法覆盖一个,但您可以获得一些相同的结果。

When you specify a constructor in the child class you can build that however you like. That constructor can then choose which super()constructor gets called.

当您在子类中指定构造函数时,您可以随意构建它。然后该super()构造函数可以选择调用哪个构造函数。

So you can block some child constructors, you can modify the parameters to them, etc. The only limitation is that at least one super constructor mustbe called.

所以你可以阻塞一些子构造函数,你可以修改它们的参数等等。唯一的限制是必须至少调用一个超级构造函数。

A {  A(int i) {} A(String b) }

B extends A { B(int i) { super(i); doStuff() } }

C extends A { C(String b) { super(Integer.parseInt(b)) } }

So we have a class Awith two constructors.

所以我们有一个A有两个构造函数的类。

Class Bon the other hand has no Stringconstructor, and does extra stuff after the super constructor happens.

B另一方面,类没有String构造函数,并且在超级构造函数发生后会做额外的事情。

Class Chas no int constructor - but when you call the Stringconstructor in Cit actually goes to the intconstructor in A.

C没有int构造-但是当你调用String构造函数C,它实际上进入到int在构造函数中A

回答by Bathsheba

When you construct an object, any parent constructors are called before the constructor specific to that object. You cannot change this behaviour.

当您构造一个对象时,任何父构造函数都会在特定于该对象的构造函数之前被调用。你不能改变这种行为。

However, it ispossible, with some trickery, to create an object without invoking anyconstructors. After which, of course, you can perform any initialisation that you need.

但是,它可能的,一些挂羊头卖狗肉,而无需调用创建对象的任何构造函数。之后,当然,您可以执行任何您需要的初始化。

You can use objenesisto create the object for you. It's pretty low-level stuff; operating at the byte-code level.

您可以使用objenesis为您创建对象。这是相当低级的东西;在字节码级别操作。

Note that you can't do this using reflection.

请注意,您不能使用反射来做到这一点。

回答by deepak tiwari

overiding defines same name with parameter type, constructor overriding defins same named constructor in parent as well as in child which defines same named parent and child class which can't be possible ..so no we can not do constructor overriding hence we can do constructor overloading.

覆盖定义了与参数类型相同的名称,构造函数覆盖定义了父类中的同名构造函数以及子类中定义了相同命名的父类和子类,这是不可能的..所以不,我们不能覆盖构造函数,因此我们可以做构造函数超载。