Class constructor example

xiaoxiao2021-03-06  76

//Initializationtest.java

Package FoldConstructor; // / / The definition of the parent class Class Parent {INT PM1;

INT PM2 = 10;

INT PM3 = Pmethod (); {System.out.Println ("Parent's Instance Initialize Block";

Public static int spm1 = 10; static {system.out.println ("Parent's Static Initialize Block";

Parent () {system.out.println ("Parent's Default Constructionor");

Static void staticmethod () {system.out.println ("parent's staticmethod");}

Int Pmethod () {system.out.println ("Parent's Method"); Return 3;}}

Class Child Extends Parent {INT CM1;

INT CM2 = 10;

INT cm3 = cmethod ();

Other Co;

Public static int scm1 = 10; {system.out.println ("Child's Instance Initialize Block");} static {system.out.println ("Child's Static Initialize Block);}

Child () {co = new other (); system.out.println ("Child's Default Construction");

Child (int M) {this (); cm1 = m; system.out.println ("child's self-define constructor");}

Static void staticMethod () {system.out.println ("Child's StaticMethod");

INT cmethod () {system.out.println ("Child's Method"); return 3;}

}

Class Other {INT OM1;

Other () {system.out.println ("Other's default constructor";

}

Public class initializationTest {public static void main (string args []) {child c; system.out.println ("program start"); // system.out.println (child.scm1); // child.StaticMethod () ; C = new child (10); child.staticMethod (); system.out.println ("program end");}}

// Continental compilation through Java's Javap:

// javap -c -classpath. child

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

New Post(0)