The common content of different objects is defined in the construction code
Function: Initializes the object, and the object is run as soon as it is established and takes precedence over the constructor function . Is the uniform initialization constructor for all objects class for the corresponding object.
1 Public classObject_structurecode {2 Public Static voidMain (string[] args) {3Person p=NewPerson ();4 //p.setname ("Libushi");5 //System.out.println (P.getname ());6Person p1=NewPerson ("Haungjianfeng");7 8Person p2=NewPerson ("Haungjianfeng", 21);9 }Ten } One classperson{ A Private intAge ; - PrivateString name; - //Building Code Blocks the { - speak (); - cry (); - } + - Person () { + //System.out.println ("A:name=" +name+ ", age=" +age); A } at Person (String name) { - This. Name =name; - //System.out.println ("B:name=" +name+ ", age=" +age); - - } -Person (String name,intAge ) { in This. Name =name; - This. age=Age ; to //System.out.println ("C:name=" +name+ ", age=" +age); + - } the //after you have the constructor, you also define the Set,get method for changing the name later * Public voidSetage (intAge ) { $ if(0<age && age<130){Panax Notoginseng This. Age =Age ; - } the Else +System.out.println ("Feifa Age"); A } the + Public intGetage () { - returnAge ; $ } $ - Public voidsetName (String name) { - This. Name =name; the } - PublicString GetName () {Wuyi returnname; the } - Wu - Public voidspeak () { AboutSystem.out.println ("Name=" +name+ ",, age=" +Age ); $ //System.out.println ("Name=" +this.name+ ", age=" +this.age); - } - Public voidcry () { -System.out.println ("Cry ..."); A } +}
Object-oriented construction code block