<java-Object Oriented (b) >

Source: Internet
Author: User

 Packagethis_demo01;/** * Student class Template: * keywords in java: * This: * Call properties: * this.  Property Name: * The authority variable is the same as the member variable name, and the This keyword is used to represent the member variable * if the local variable and member variable names are different, this can be omitted * Call Method: * this. method ();     * This * call constructor can be omitted in this class: * this ();  Call the parameterless construct * This ("Kid", 2);   Call a parameter construct * This statement must be in the constructor first sentence * Static can be decorated: * "1" Property--static variable /class variable--defined in class * "2" Method--static method/class method--defined in class * "3" code block-static code block                     * "4" inner class--Static inner class * * Static modifier properties, methods, etc. are loaded into memory when the class is loaded * How to use: * The class name. Property name/Method name () * Object name. Attribute name/Method name () * Note: * Static method can only use or invoke static decorated variable or call Stati The C method * Cannot use a property that is not static decorated (member genus* While the static method is generated when the class is loaded, there are no objects in the shared zone * loaded into memory in the static method, so you cannot use a non-static method to not only invoke non-static properties and methods but also You can call static methods and properties * because the resulting can be called first.  *      *@authorWeguikin **/ Public classStudent {//Writing Properties    Private intIdnum;//School Number    PrivateString name;//name    Private CharGender//Sex//private String City; //location     Public StaticString City;//Location--//non-parametric construction     PublicStudent () {}//with a reference structure     PublicStudent (intIdnum,string name,Chargender,string City) {         This("Zhenjiang", ' Male '); //This (); //Call no parameter         This. Idnum =Idnum;  This. Name =name;  This. Gender =gender;  This. City =City ; }     PublicStudent (String name,Chargender) {         This. Name =name;  This. Gender =gender; }            //the public value assignment//School Number     Public intGetidnum () {returnIdnum; }     Public voidSetidnum (intidnum) {         This. Idnum =Idnum; }    //name     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    //Sex     Public CharGetgender () {returngender; }     Public voidSetgender (Chargender) {         This. Gender =gender; }    //City     PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }        //Method     Public voidInit () {System.out.println ("I am study number for:" +getidnum () + "" +getname () + ", Gender:" +getgender () + ", From:" +getcity ()); }}
 Packagethis_demo01;/*** Test class *@authorWeguikin **/ Public classTest { Public Static voidMain (string[] args) {//Creating ObjectsStudent su =NewStudent (123421, "West gram", ' Male ', "Africa"); Su.        Init ();   System.out.println (SU); //output is: memory addresssu.city= "Shanghai";//The object name. Propertiesstudent.city = "Beijing";//class name. PropertiesSystem.out.println (student.city);//             }}

04. Code BLOCK:

Package the_code_block_demo01;/** code BLOCK: * Concept: Use {} A section of code * classification: * Common code block code defined directly in a method or statement Block * Constructs code block directly written in class code block * Static code block using static declaration of code block * Same as Step code block multi-threaded object when speaking * * Execution Flow: * Static code blocks--load once * construct code blocks--Each creation of an object executes a Times * Construction method once per creation of an object * @author Weguikin **/ Public classDemoblock {Static intCity ;  PublicDemoblock () {System. out. println ("========== I am the construction method ========="); }    //"2" Construction code block{System. out. println ("============ I'm building a code block ============="); }        //"3" Static code block    Static{ City= A; System. out. println ("============ I am a static code block ==============="); }     Public Static voidMain (string[] args) {//Creating ObjectsDemoblock sum =NewDemoblock (); //"1" Ordinary code block        {            //code defined in a method            intAge = -; System. out. println (age); }         for(inti =1; I <Ten; i++) {            {                intAge = -; System. out. println (age); }        }    }}

<java-Object Oriented (b) >

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.