The Java is different from the C # constructor execution order.

xiaoxiao2021-03-06  43

Java: Execute the constructor of the parent class first, then the constructor of the reference object, then it is its own constructor. Public class test {public static void main (string [] args) {child child = new child ();}}

Class Parent {parent () {system.out.println ("to construct parent.");}}

Class Child Extends Parent {Child () {System.out.println ("To Construct Child."); DELEGATEE DELEGATEE = New Delegatee ();

Class delegatee {delegatee () {system.out.println ("to construct delegatee.");}} The result is to construct parent.to construct delegatee.to construct child.c #: first reference objects, in the parent class, recoil .using System; namespace ConsoleApplication1 {public class Test {public static void Main (String [] args) {Child child = new Child ();}} class parent {public parent () {Console.WriteLine ( "to construct parent") ;}} class Child: Parent {public Child () {Console.WriteLine ( ". to construct Child");} Delegatee delegatee = new Delegatee ();} class Delegatee {public Delegatee () {Console.WriteLine ( "to construct ");}}} The result is to construct delegatee.to Construct Child.to Construct Parent. Summary: The prior constructed of the dependence is dependent on the post-constructor. Java is a cross-layer dependence preferred to depends on the same level, and C # is the same level depending on the cross-layer dependence.

转载请注明原文地址:https://www.9cbs.com/read-63489.html

New Post(0)