Object-oriented construction code block

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.