简体   繁体   English

如果对象是超类且无参数构造函数具有空体,那么默认构造函数如何用于实例化对象?

[英]How does default constructor used to instantiate object if it's super class no argument constructor has empty body?

default constructor invoke no_argument constructor in super class reference and this last constructor used to instantiate object from this class, How ?默认构造函数在超类引用中调用 no_argument 构造函数,最后一个构造函数用于从此类实例化对象,如何 i mean what does superclass constructor body exactly do ?我的意思是超类构造函数体到底是做什么的?

example例子

public class Child extend Fragment {}

if i tried to instantiate this class Child如果我试图实例化这个类 Child

Child a = new Child() ;

while compiling compiler invoke no-argument constructor in Fragment class .在编译编译器时调用 Fragment 类中的无参数构造函数。 in android.app package i found that no-argument constructor has no body在 android.app 包中,我发现无参数构造函数没有主体

public Fragment() {}

Question how could this constructor be useful ?问题这个构造函数如何有用? it do nothing !它什么都不做!

PS i mean by PS我的意思是

how could this constructor be useful?这个构造函数怎么会有用呢?

how does it instantiate its class although it empty , where is the code that instantiate super class?虽然它是空的,它如何实例化它的类,实例化超类的代码在哪里?

The syntax for calling a superclass constructor is调用超类构造函数的语法是

super();

or:或者:

 super(parameter list);

Question how could this constructor be useful ?问题这个构造函数如何有用? it do nothing !它什么都不做!

The super constructor could be used to initialized properties common for all subclasses.超级构造函数可用于初始化所有子类共有的属性。 If it's empty it still can be used by reflection API.如果它是空的,它仍然可以被反射 API 使用。

Fragment.class.getConstructor().newInstance();

or或者

Child a = new Child() ;
a.getClass().getSuperclass().getConstructor().newInstance();

the last line explicitly invokes a constructor of the superclass.最后一行显式调用超类的构造函数。

Question how could this constructor be useful ?问题这个构造函数如何有用? it do nothing !它什么都不做!

The purpose of a constructor, whether empty or not, is to provide an instance of the class.构造函数的目的,无论是否为空,都是提供类的实例。 The constructor body, if it exists, may further do additional operations necessary when first creating the instance.构造函数体(如果存在)可能会在首次创建实例时进一步执行其他必要的操作。

The constructor may not be doing anything itself but it returns an instance, on which you may further call public methods defined in the class.构造函数本身可能不做任何事情,但它返回一个实例,您可以在该实例上进一步调用类中定义的公共方法。

For example, in your mentioned Fragment class, let's suppose there is an attach() method.例如,在您提到的Fragment类中,假设有一个attach()方法。 If the attach() method is non-static, you will need to create an instance (using the empty constructor) to call the attach() method on it irrespective of whether the constructor does anything or not.如果attach()方法是非静态的,则无论构造函数是否执行任何操作,您都需要创建一个实例(使用空构造函数)来调用其上的attach()方法。

how does it instantiate its class although it empty尽管它是空的,它如何实例化它的类

A constructor doesn't need any code in its body to be instantiated.构造函数不需要在其主体中实例化任何代码。 Calling new ClassName() (with appropriate parameters, if any) is enough.调用new ClassName() (使用适当的参数,如果有的话)就足够了。 It is the job of JVM to create the object at runtime.在运行时创建对象是 JVM 的工作。 You don't need to write any specific code inside a constructor for that.您不需要为此在构造函数中编写任何特定代码。 That extra code, if you wish, can help the instance start off with a specific state by setting some fields or calling methods, etc.如果您愿意,额外的代码可以通过设置一些字段或调用方法等来帮助实例以特定状态开始。

where is the code that instantiate super class?实例化超类的代码在哪里?

A constructor in a subclass (or any class, for that matter) only generates an instance of that class.子类(或任何类,就此而言)中的构造函数仅生成该类的实例。 It doesn't need to instantiate its super class.它不需要实例化它的超类。 You may call the super class constructor (using super() ) if it has some important initialization code, but it's not compulsory.如果它有一些重要的初始化代码,您可以调用超类构造函数(使用super() ),但这不是强制性的。 Also, subclasses don't inherit constructors from super classes.此外,子类从超类继承构造函数 But they do inherit the public and protected fields and methods.但它们确实继承了公共和受保护的字段和方法。

Regarding the Fragment class, you have have some confusion regarding its use which can be clarified here .关于Fragment类,您对其使用有一些困惑,可以在此处澄清。

an empty constructor (aka default constructor) means only that an instance can be created without any further work.空构造函数(又名默认构造函数)意味着只能创建一个实例而无需任何进一步的工作。 This can be due to framework requirements or the instantation is ok with the default values for the attributes.这可能是由于框架要求或属性的默认值的实例化是可以的。

If the empty constructor is the only constructor in the class, it is not required to write it, its there by default.如果空构造函数是类中唯一的构造函数,则不需要编写它,默认情况下它就在那里。

Although if there are other constructors (with parameters), one must explicitly state the default constructor.尽管如果有其他构造函数(带参数),则必须明确说明默认构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实例化具有带有参数的私有构造函数的泛型类 - How to instantiate generic class which has a private constructor with an argument 如何实例化具有需要接口对象的构造函数的类 - How to instantiate a class that has a constructor requiring an interface object 如何拦截超类构造函数参数? - How to intercept super class constructor argument? 调用子类构造函数的超类对象? 这是如何运作的? - Super class object invoking a subclass constructor? How does that work? 即使子类已经有一个已定义的构造函数,父类是否总是需要一个默认或无参数的构造函数? - Does a parent class always need a default or no-argument constructor even if the child class already has a defined constructor? 调用超类的无参数构造函数 - Calling no argument constructor of super class 创建该 class 的空 object 的默认构造函数 - Default constructor that creates an empty object of that class 为什么MyClass中的默认构造函数调用Object类的超级ie - why does the default constructor in MyClass calls super i.e of Object class Java - 如何根据继承类的构造函数参数调用不同的super()? - Java - how to call different super() according to inheriting class's constructor argument? Child-class构造函数中的super是什么? - What does super(s) in Child-class constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM