java 调用构造函数是否意味着创建对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14453270/
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
Does invoking a constructor mean creating object?
提问by NINCOMPOOP
When we create a Subclass object which extends an abstract class, the abstract class constructor also runs . But we know we cannot create objects of an abstract class. Hence does it mean that even if a constructor completes running without any exception, there is no guarantee whether an object is created?
当我们创建一个扩展抽象类的子类对象时,抽象类构造函数也运行 . 但是我们知道我们不能创建抽象类的对象。那么是不是意味着即使构造函数无异常地完成运行,也不能保证是否创建了对象?
回答by Rohit Jain
Hence does it mean that even if a constructor completes running without any exception, there is no guarantee whether an object is created?
那么是不是意味着即使构造函数无异常地完成运行,也不能保证是否创建了对象?
Simply speaking, a constructor
does not create an object. It just initializesthe stateof the object. It's the new
operator which creates the object. Now, let's understand this in little detail.
简单地说,aconstructor
不创建对象。它只是初始化对象的状态。它是new
创建对象的运算符。现在,让我们稍微详细地了解一下。
When you create an object using statement like this:
当您使用这样的语句创建对象时:
new MyClass();
The object is first created by the new
operator. Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object.
对象首先由new
操作员创建。就在对新创建的对象的引用作为结果返回之前,处理指示的构造函数以初始化新对象。
Now consider the case of Abstract class
and it's concrete SubClass
, when you do like this:
现在考虑Abstract class
和 具体的情况SubClass
,当你这样做时:
AbstractClass obj = new ConcreteClass();
new
operator creates an object of ConcreteClass
, and invokes its constructor to initialize the state of the created object. In this process, the constructor of the abstract class is also called from the ConcreteClass
constructor, to initialize the state of the object in the abstract class.
new
运算符创建 的对象ConcreteClass
,并调用其构造函数来初始化所创建对象的状态。在这个过程中,抽象类的ConcreteClass
构造函数也从构造函数中调用,初始化抽象类中对象的状态。
So, basically the object of AbstractClass
is not created. It's just that it's constructor is invoked to initialize the state of the object.
所以,基本上AbstractClass
没有创建的对象。只是调用了它的构造函数来初始化对象的状态。
Lessons Learnt:
得到教训:
The object is created by
new
operator, and not by the invocation of the constructor itself. So, the object is already created before any constructoris invoked.Constructor is just used to initialize the state of the object created. It does not create an object itself.
An object state can also be contained in an abstract super class.
- So, the purpose of invocation of
Abstract class
constructor, is only to initialize the object completely, and no object is created in process.
对象是由
new
操作符创建的,而不是由构造函数本身的调用创建的。因此,在调用任何构造函数之前已经创建了对象。构造函数只是用来初始化所创建对象的状态。它本身不创建对象。
对象状态也可以包含在抽象超类中。
- 所以,调用
Abstract class
构造函数的目的,只是为了完全初始化对象,过程中并没有创建对象。
See:
看:
回答by Azodious
But we know we cannot create objects of an Abstract class
但是我们知道我们不能创建抽象类的对象
Right but JVM can.
是的,但 JVM 可以。
does it mean that even if a constructor completes running without any exception , there is no guarantee whether an object is created ?
这是否意味着即使构造函数无异常地完成运行,也不能保证是否创建了对象?
The object is definitely created internally.
该对象肯定是在内部创建的。
Does invoking a constructor mean creating object?
调用构造函数是否意味着创建对象?
Not always. you can invoke constructor using super()
and this()
but it won't instantiate an object. (but will just invoke the constructor)
不总是。您可以使用super()
and调用构造函数,this()
但它不会实例化对象。(但只会调用构造函数)
class AClass
{
AClass()
{
this(1); // will invoke constructor, but no object instatiated.
}
AClass(int a)
{
}
public static void main(String[] args)
{
AClass obj = new AClass(); // one object instantiated.
}
}
回答by rai.skumar
Subclass == BaseClass + Extras you add in sub class
Subclass == BaseClass + Extras you add in sub class
Thus when you create a subclass by calling its constructor, there is a call to base class constructor as well to make sure that all attributes (of the base class) are also properly initialized.
因此,当您通过调用其构造函数创建子类时,还会调用基类构造函数以确保(基类的)所有属性也已正确初始化。
回答by Jeff Bowman
Barring any exceptions, the abstract class constructor is only run from within the subclass's constructor (as the first statement). Therefore you can be sure that every time a constructor is run, it is in the process of creating an object.
除非有任何异常,抽象类构造函数只在子类的构造函数中运行(作为第一条语句)。因此,您可以确定每次运行构造函数时,它都在创建对象的过程中。
That said, you may call more than one constructor in the process of creating a single object, such as Subclass()
calling Subclass(String)
which calls AbstractClass
via a super()
call, and so forth.
也就是说,在创建单个对象的过程中,您可能会调用多个构造函数,例如通过Subclass()
调用调用Subclass(String)
which等等。AbstractClass
super()
回答by Theodoros Chatzigiannakis
When you invoke a constructor using new
, a new object is being created.
当您使用 调用构造函数时new
,将创建一个新对象。
Now, as you probably already know, every constructor of any subclass, either implicitly or explicitly, directly or indirectly, invokes a constructor from the parent class (which, in turns, invokes one from its ownparent class, all the way up to object). This is called constructor chaining.
现在,您可能已经知道,任何子类的每个构造函数,无论是隐式的还是显式的,直接或间接的,都从父类调用一个构造函数(反过来,它又从自己的父类调用一个构造函数,一直到 object )。这称为构造函数链接。
The above, however doesn't mean that multiple objects are created. The object has been created at the new
call and all constructors working on that object are already handed an allocated area. Therefore, constructor chaining does not create new objects. One call to new
will return you one object.
然而,以上并不意味着创建了多个对象。该对象已在new
调用时创建,并且在该对象上工作的所有构造函数都已分配给一个分配区域。因此,构造函数链接不会创建新对象。一次调用new
将返回一个对象。
回答by Amit Gautam
I am completely disagree that the object for an abstract class can not be created and only jvm can does it in case of Inheritance even a programmer can do it a t times whenever or wherever he/she intends to do so by using the concept of anonymous class: Look at this code and try it by your Own
我完全不同意抽象类的对象不能被创建,只有 jvm 可以在继承的情况下做到这一点,即使是程序员也可以通过使用匿名类的概念在他/她打算随时随地这样做的时候做到这一点: 看看这段代码,自己试试
abstract class Amit{
void hai()
{System.out.print("Just Wanna say Hai");}
abstract void hello();
}
class Main{
stic public void main(String[]amit)
{
Amit aa=new Amit(){
void hello(){Sstem.out.print("I actually dont say hello to everyone");
}};
aa.hello(); }}
回答by NiranjanBhat
This is how the flow works when you invoke the constructor of your subclass:
当您调用子类的构造函数时,流程是这样工作的:
- Constructor of Abstract class runs --> the object is half initialized here.
- Constructor of subclass finishes execution --> The object is fully initialized and hence completely created here.
- Abstract 类的构造函数运行 --> 对象在这里被初始化了一半。
- 子类的构造函数完成执行 --> 对象已完全初始化,因此在此处完全创建。
回答by Karthik T
You can only call the abstract class constructor as a part of a concrete subclass constructor. This is OK, since the abstract class is extended into a concrete classand it is an object of that concrete classthat is being created.
您只能将抽象类构造函数作为具体子类构造函数的一部分来调用。这是可以的,因为抽象类被扩展为具体类,并且它是正在创建的具体类的对象。