Java Learning--the law of landing for object-oriented thought

Source: Internet
Author: User
Tags float double modifiers

* Object-oriented thinking of the ground rule one:
* 1. Design the class and design the members of the class (member variables & member methods)
* 2. Create the Class object (also known as instantiation of the class) through the class
* 3. Call through "object. Properties" or "object. Method" to complete the corresponding function
 *
* Two, create multiple objects, each with a set of properties of the class. When you modify the properties of one of the objects,
* Does not affect the property values of other objects.
 *
* Three, class attributes (member variables)
* Member variables vs local Variables
* Same point: 1. Follow the format of the variable declaration: Data type variable name = Initialize value
* 2. There are scopes
* Different points: 1. Different location of declaration: Member variable: declaration in class, outside method
* Local Variables: Declared within a method, the formal parameter part of a method, within the code block
* 2. The modifier for the member variable has four: Public private protected default
* Local variables have no modifiers, same as the method modifiers in which they are located.
* 3. Initialization value: There must be an initialization value.
* Member variables: If you do not explicitly assign a value at the time of declaration, the different data types will have different default initialization values.
* byte short int long ==>0
* Float Double ==>0.0
* Char ==> space
* Boolean ==>false
* Reference type variable ==>null
* Local Variables: Be sure to assign the values explicitly. (Local variables do not have default initialization values)
* 4. The two locations in memory are different: member variables exist in heap space; local variables: in stack space
 *        
* Summary: About the categories of variables: 1) by Data type: Basic data Type (8) & Reference data type
* 2) vary by location: member variables & local Variables
 *
* Four, class method: To provide a function of the implementation
* 1) Example: public void Eat () {//Method body}
* Public String GetName () {}
* public void SetName (String n) {}
* Format: Permission modifier return value type (void: no return value/specific return value) method name (formal parameter) {}
 *     
* 2) about return value type: void: Indicates that this method does not require a return value
* Method with return value: At the end of the method there must be a return + variable corresponding to the return value type
* Memory: Void and return can not appear in a method at the same time. Like a pair of "friends."
 *
* 3) You can call other methods or properties of this class within a method, but you cannot define a method within a Method!
 *
 Public classZoo { Public Static voidMain (string[] args) {//declaration of the base data type: Data type variable name = initialization value        inti = 10; //instantiation of a class: The following A1 is a real objectAnimal A1 =NewAnimal (); //int[] arr = new INT[10];System.out.println ("Name:" + A1.name + "Age:" +a1.age); //calling properties by ObjectA1.name = "Floral"; A1.age= 3; System.out.println ("Name:" + A1.name + "Age:" +a1.age); //calling methods from objectsa1.eat ();                A1.sleep (); //to create an object of a class againAnimal A2 =NewAnimal (); System.out.println ("Name:" + A2.name + "Age:" + a2.age);//NULL 0A2.name = "Floret"; System.out.println ("Name:" + A1.name + "Age:" +a1.age); System.out.println ("Name:" + A2.name + "Age:" +a2.age); //A3 does not mean that a A1 object is recreated, but rather that A1 and A3 share an object entityAnimal A3 =A1; System.out.println ("Name:" + A3.name + "Age:" + a3.age);//same as the A1 .A3.name = "Pooh Bear"; System.out.println ("A1:name:" + A1.name + "Age:" +a1.age); System.out.println (A2.getname ());//A2.name;System.out.println (A2.desc ()); }}classanimal{//1. PropertiesString name; intAge ; //2. Methods     Public voideat () {System.out.println ("Animal Feeding"); }         Public voidsleep () {System.out.println ("Animal dormancy"); //return;    }         PublicString GetName () {returnname; }     Public intGetage () {System.out.println ("Hello"); returnAge ; //statements cannot be declared thereafter//System.out.println ("Hello");    }    //When this method is called through an object, the return value of the method's method is provided to the caller of the method, which is the current object.      PublicString desc () {if(Age > 2){            return"Just the boys."; }Else{            return"Or the Age of Cartoons"; }    }     Public voidSetName (String N) {//N: Local variablesName =N; }     Public voidaddage () {inti = 0;//Local VariablesAge + =i; }     Public voidinfo () {//you can call other methods of this class within a method, but you cannot define a new method within a methodeat (); Sleep ();//Public void Breath () {//System.out.println ("Breathing");//        }    }//System.out.println ("hello!");}

Java Learning--the law of landing for object-oriented thought

Related Article

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.