简体   繁体   English

如果我们创建子类的对象,是否会创建父类的对象?

[英]will there be object creation of parent class if we create an object of child class?

I know that super will be called every time we create a child class object. 我知道每次创建子类对象时都会调用super。 But, what I specifically want to know is that will it load the class or will it create the object of that parent class. 但是,我特别想知道的是它会加载类还是会创建该父类的对象。

Thanks in advance. 提前致谢。

You have a single object. 你有一个对象。
As a class is instantiated, it invokes first the constructor of the parent class. 在实例化类时,它首先调用父类的构造函数。 And for each parent class, it follows the same logic : it invokes first the constructor of the parent class. 对于每个父类,它遵循相同的逻辑:它首先调用父类的构造函数。

Suppose : A extends B extends C . 假设: A扩展B扩展C

new A() results to the B constructor invocation that invokes itself the C constructor. new A()导致B构造函数调用,它自己调用C构造函数。
As C then B constructor invocations are returned, the A constructor can go on and the instance A is created. 作为C然后返回B构造函数调用, A构造函数可以继续并创建实例A

But, what I specifically want to know is that will it load the class or will it create the object of that parent class. 但是,我特别想知道的是它会加载类还是会创建该父类的对象。

Similarly to the constructor execution, before loading a class, its parent class has to be loaded first and that recursively for each parent class. 与构造函数执行类似,在加载类之前,必须first加载其父类,并为每个父类递归加载。

Here classes corresponding to A extends B extends C : 这里对应于A类扩展B扩展C

class C {
    public C(){    
        System.out.println("C constructor");
    }
}

class B extends C{
    public B() {
        System.out.println("B constructor");
    }

}
class A extends B{
    public A() {
        System.out.println("A constructor");
    }
}


public class Test {
    public static void main(String[] args) {
        new A();
    }
}

Compile these classes and then execute the Test class that creates a A instance in its main() method by specifying the -verbose flag (enable by default the classes logs) in the java command : 编译这些类,然后通过在java命令中指定-verbose标志(默认情况下启用类日志)来执行在其main()方法中创建A实例的Test类:

java -verbose Test

And you should see in the output standard something like : 你应该在输出标准中看到类似的东西:

[Loaded java.lang.Object from C:\...]
[Loaded java.io.Serializable from C:\...]
... // load all classes required until loaded C, B and A classes
[Loaded C from file:/C:/...]
[Loaded B from file:/C:/...]
[Loaded A from file:/C:/...]
C constructor
B constructor
A constructor

It confirms that : 它证实:

  • the parent classes need to be loaded first by starting by the root parent class ( Object ) until the closest parent class( B ). 需要首先通过根父类( Object )开始加载父类,直到最近的父类( B )。
  • no constructor is executed before all classes of the hierarchy be loaded 在加载层次结构的所有类之前,不执行构造函数
  • the constructors of the parent classes are always executed before the constructor of the instantiated class by starting by the root parent constructor ( Object ) until the closest parent constructor( B ). 父类的构造函数总是在实例化类的构造函数之前执行,方法是从根父构造函数( Object )开始,直到最接近的父构造函数( B )。

The constructor is called, therefore an object of the parent class is created. 调用构造函数,因此创建父类的对象。 That object is used later to build an object of the child class. 稍后使用该对象来构建子类的对象。 The reason for this is that an object of the child class is an object of the parent class with more things. 这样做的原因是子类的对象是父类的对象,具有更多的东西。

Because the child class inherits properties and methods from the parent class, the entire class hierarchy (including superclasses and implemented interfaces) are loaded . 因为子类从父类继承属性和方法,所以加载了整个类层次结构(包括超类和实现的接口)。

When you create an instance of the childclass, only a single object of type childclass is created (instantiated). 当您创建childclass的一个实例,只有类型childclass的一个对象被创建(实例化)。 There are no additional instances of any parent(s). 没有任何父母的额外实例。

The properties and methods declared on parent classes are accessible via the childclass instance, and due to this inheritance the childclass instance will be accepted as argument to any methods that expect an instance of the parent (or any interfaces that it or any parent implements). 在父类上声明的属性和方法可以通过子类实例访问,并且由于这种继承,子类实例将被接受为任何期望父实例(或它或任何父实现的任何接口)的方法的参数。

If you think of the code as a contractual design , this makes perfect sense: 如果您将代码视为合同设计 ,那么这非常有意义:

  • The parent class declaration establishes that certain methods are available to call on itself or any of its children. 父类声明确定某些方法可用于调用自身或其任何子代。 The same is true for interfaces. 接口也是如此。
    For example , all employment contracts in the real world must contain certain clauses, but the rest is up to the company. 例如 ,现实世界中的所有雇佣合同都必须包含某些条款,但其余的由公司决定。
  • By inheriting ( extends or implements ) it is guaranteed that the child will also automatically provide the same methods. 通过继承( extendsimplements ),可以保证子项也将自动提供相同的方法。
    For example , we can assume that any law-abiding company will provide employment contracts including those clauses and (once agreed) the signature of both you and the company. 例如 ,我们可以假设任何遵守法律的公司将提供雇佣合同,包括这些条款和(一旦同意)您和公司的签名。
  • The entire hierarchy needs to be loaded in order to understand the full contract. 需要加载整个层次结构才能理解完整的合同。
    For example , if there is a dispute, the lawyers who examine a contract will check what the law prescribes, perhaps by looking at a sample contract provided by the legislating body. 例如 ,如果存在争议,审查合同的律师将通过查看立法机构提供的样本合同来检查法律规定的内容。 The parentClass is that sample in the problem. parentClass是问题中的样本。
  • We only need one object instance. 我们只需要一个对象实例。
    For example you won't get two employment contracts, one for the basic stuff and another with more information. 例如,你不会得到两份劳动合同,一份是基本的,另一份是有更多的信息。 You will receive and sign a single document. 您将收到并签署一份文件。
  • Finally, in my analog, any employment contract (no matter how it is phrased) will be acceptable to any agency that asks you to prove your relationship to the company that employs you (assuming it is legal and valid by containing the legally prescribed clauses and appropriate signatures). 最后,在我的模拟中,任何雇佣合同(无论如何措辞)都会被任何要求您证明您与雇用您的公司的关系的机构所接受(假设它是合法且有效的,包含法律规定的条款和适当的签名)。

When you first load a class, all its ancestors are also loaded recursively. 当您第一次加载一个类时,它的所有祖先也会以递归方式加载。

When you instantiate an object you won't instantiate an additional instance of its parent. 实例化对象时,不会实例化其父对象的其他实例。 However, the object you just instantiated is itself also an instance of the parent. 但是,您刚刚实例化的对象本身也是父实例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM