英文:
Java constructors and super()
问题
1. 场景: 我的类FotoApparat
没有自定义构造函数,也没有任何构造函数,我使用FotoApparat meinFotoApparat = new FotoApparat()
创建了一个FotoApparat
的实例。
问题: 因为我的类没有构造函数,也没有super()
调用,我猜程序会检查父类Object
是否有合适的构造函数,这个构造函数应该是new Object()
,对吗?如果是的话,这仍然被视为“隐式”的super()
调用吗?
2. 场景: 我创建了一个自定义构造函数(通过使用Eclipse源代码),它带有参数。在生成的构造函数中,super()
调用被添加在最开始,我猜这就是我一直在阅读的实际隐式调用。我在javapoint上阅读到,当创建一个类的实例时,父类的实例也会被创建,这个实例由super()
引用。
问题: 我读到说这个super()
调用可以从构造函数中移除,但是如果它被移除了,而我又使用一个带有参数的构造函数,那么(没有super()
的情况下)这个父对象是如何被创建的呢?
英文:
I have seen a few questions on the topic, but they all assume knowledge of inheritance. The example in my book is before the inheritance chapter, so the parent class is java.long.Object.
1. Scenario: my class FotoApparat has no custom constructor or any constructor at all and I create an instance of FotoApparat with FotoApparat meinFotoApparat = new FotoApparat()
Question: As my class has no constructor and also no super() call, I assume the program checks the parent Object class for a suitable constructor, which should be new Object(), right? If yes, is this still considered an "implicit" super() call?
2. Scenario: I create a custom constructor (by using eclipse source) which takes on parameters. In the generated constructor the super() call is added in the very beginning, which I assume is the actual implicit call I keep reading about. I read on javapoint that when an instance of a class is created, an instance of the parent class is also created, which is referenced by super().
Question: I read that this super() call can be removed from the constructor, but if it is removed and I use a constructor that takes on parameters, then (without super()) how is this parent object created ?!
答案1
得分: 4
场景 1:
如果你没有定义任何构造函数,系统会为你创建一个默认的无参构造函数。当使用 new FotoApparat()
时就会调用这个默认构造函数。这个默认构造函数会隐式地调用 Object
上的构造函数(参见场景 2)。
场景 2:
如果你没有显式调用 super()
,系统仍会隐式地进行这个调用。但是有可能父对象没有无参构造函数,这种情况下你就必须调用一个特定的构造函数。
英文:
Scenario 1:
If you don't define any constructor, a default, no-argument, constructor is created for you. That's the one that is called when using new FotoApparat()
. This default constructor then calls the constructor on Object
(see scenario 2.)
Scenario 2:
If you don't explicitly call super()
, this call is still done implicitly. It is possible however that the parent object does not have a constructor without arguments, in which case you are required to call a specific constructor.
答案2
得分: 1
> 由于我的类没有构造函数,也没有space()调用,我认为程序会检查父Object类是否有合适的构造函数。
并不完全准确。如果你没有定义构造函数,编译器会为你创建一个。这个构造函数不接受任何参数,它唯一要做的就是调用父类构造函数 super()
。
> 当创建类的实例时,也会创建父类的实例。
不完全正确:只会创建一个实例。没有单独的父类实例。
这个说法不完全准确,因为通过继承,所创建的子类实例也是父类的一个实例。
> 我读到这个super()调用可以从构造函数中删除,但如果删除了,并且我使用一个带有参数的构造函数,那么(没有super())这个父对象如何被创建呢?
在这种情况下,编译器会插入一个调用无参的父类super构造函数 super()
。但这不会创建一个单独的“父对象” - 只会创建一个对象。
你的学习可能没有很清楚地表明对象的创建和初始化之间的区别。调用构造函数并不会“创建”一个对象。对象是通过在内存中为其保留空间来创建的。在内存空间被保留后,构造函数被调用来“初始化”这个对象。
英文:
> As my class has no constructor and also no space() call, I assume the program checks the parent Object class for a suitable constructor
Not quite. If you don't define a constructor, the compiler creates one for you. This constructor takes no arguments, and the only thing it does is call the super class constructor super()
.
> when an instance of a class is created, an instance of the parent class is also created
Not quite: only one instance is created. There is no separate parent class instance.
The statement is not entirely incorrect because thanks to inheritance, the one instance of the child class that is created is also an instance of the parent class.
> I read that this super() call can be removed from the constructor, but if it is removed and I use a constructor that takes on parameters, then (without super()) how is this parent object created ?!
In this scenario the compiler inserts a call to the no-argument super class constructor super()
. But this does not create a separate "parent object" - only one object is created.
What your studies may not have made clear is the distinction between object creation and initialization. Calling a constructor does not "create" an object. An object is created by reserving space for it in memory. After the memory has been reserved, the constructor is called to "initialize" the object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论