Java 隐式构造函数和显式构造函数有什么区别?

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

What is the difference between implicit constructor and explicit constructor?

java

提问by user3627332

Can some one explain me the difference between implicit constructor and explicit constructor in Java?

有人可以解释一下Java中隐式构造函数和显式构造函数之间的区别吗?

回答by Jawad Zeb

Explicitmeans done by the programmer. Implicitmeans done by the JVM or the tool , not the Programmer.

显式意味着由程序员完成。 隐式意味着由 JVM 或工具完成,而不是由程序员完成。

For Example:

例如:

Java will provide us default constructor implicitly.Even if the programmer didn't write code for constructor, he can call default constructor.

Explicit is opposite to this , ie. programmer has to write .

Java会隐式地为我们提供默认构造函数。即使程序员没有为构造函数编写代码,他也可以调用默认构造函数。

Explicit 与此相反,即。程序员必须写。

Default constructoris the constructor with no arguments requested. It is called implicitly when creating an instance.

默认constructor是带有 的构造函数no arguments requested。它在创建实例时被隐式调用。

The no-args constructoris called implicitly if you don't call one yourself, which is invalid if that constructor doesn't exist. The reason it is required to call a super constructor is that the superclass usually has some state it expects to be in after being constructed, which may include privatevariables that can't be set in a sub-class. If you don't call the constructor, it would leave the object in a probablyinvalid state, which can cause all kinds of problems

no-args constructor如果您自己不调用,则隐式调用 ,如果该构造函数不存在,则这是无效的。需要调用超构造函数的原因是超类通常有一些它期望在 be 之后处于的状态constructed,其中可能包括private不能在子类中设置的变量。如果你不调用constructor,它会使对象处于probably无效状态,这会导致各种问题