Big Data <javase + Linux Elite Training class >_day_08

Source: Internet
Author: User

1: Object-oriented and process-oriented ideas are        a way for us to write programs.                process-oriented: this way of programming, encountering things to think about:"What should I Do", and then step by step implementation of the process.                        The typical C language is.        Object-oriented: This way of programming, encounter things thinking:"I change who to do", and then who is the object.                      how he would do it was his own business, but in the end it would be all right to do it together.                      If you can't find anyone, create this person. The Java language is.

2: Object-oriented implementation of code

The elephant has a few steps to install the refrigerator
Open the door and put the elephant in (storing the functions of elephant-and Java) close the door

Analysis:
Specific things: refrigerators, elephants
The Java language is a way of describing things in real life.

Class Refrigerator {
Open () {

}
Close () {

}
Storage elephant (Elephant D) {

}
}

Class Elephant () {

}

public static void Main (string[] args) {
Refrigerator XX = new refrigerator (); The fridge is really there.
Elephant YY = new Elephant (); Elephants exist, too.

Invoke function
XX. Open the Door ();
XX. Storage elephant (YY);
XX. Close the door ();
}

3: The difference between an object-oriented local variable and a member variable



 Packagecom.itstar.demo01; Public classCar {//part of Car: properties and Methods//need to drive it//ColorString color; //Wheels    intcount; //define the function of running     Public voidrun () {System.out.println ("The car is running" + "..." + Color + "..." +count); }}
 Package com.itstar.demo01;  Public class cartest {    publicstaticvoid  main (string[] args) {        //  Create a variable for car class        new  car ()        ; = "Red";         = 4;                C.run ();    }}
 package   om.itstar.demon02;  public  class      Car {String color;         int   count;  public  void   run () {System.out.println ( "car is running" + Color + "..." +
 package   om.itstar.demon02;  /*   * The difference between a member variable and a local variable: * * 1, define the difference in position * member variable, defined in class, outside method * Bureau Part variable, within the method, within the statement * 2, Scope different * member variable, the scope is the entire class * Local variables, within the method, within the statement * 3, the default value is different * member variable has Own default value of the * local variable has no default value, no assignment cannot use * 4, different memory location * member variable, follow object into heap memory to store * Local variables, follow their own method into the stack Memory * 5, life cycle different * member variables, following objects, stored in heap memory. The memory waits for the JVM to clean up, the life cycle is longer * Local variables, follow the method, the method executes the stack after execution. The life cycle is relatively short. *  */ public   Class   cartest {}  
 Package com.itstar.demon03;  Public class Person {    String name;}
 Packagecom.itstar.demon03; Public classPersontest { Public Static voidMain (string[] args) {intA = 1;        function (a); System.out.println (a); //1    }         Public Static voidfunction (person p) {P.name= "Weguikin"; }         Public Static voidfunctioninta) {a+ = 3; }}
 Packagecom.itstar.demon04; /** Describe this thing of man: * attribute: Name Age * Method: Speak * New keyword: Private private member modifiers cannot be decorated with local variables * be modified by private, can only be used in their own class with permission to decorate * * To private variables, provides a common way to access: Method*/ Public classPerson {//person's name, member variableString name; //person's age, member variable--security issues (will not cause the program to hang up) but it's against the real situation.    Private intAge ; //the variable age is private to us, providing a way for external classes to use     Public voidSetage (inta) {//limits on the scope of a        if(A < 0 | | a > 200) { age= 18; }Else{ Age=A; }    }        //defines the method that gets the value of the variable age using     Public intGetage () {returnAge ; }        //define the function of human speaking     Public voidspeak () {System.out.println (name+ "..." + Age + "in speech"); }}
 Packagecom.itstar.demon04;/** Create objects, objects invoke properties and methods of custom classes*/ Public classPersontest { Public Static voidMain (string[] args) {//Create a person class objectPerson p =NewPerson (); //assigning values to member variablesP.setage (42); P.name= "Huern";                P.speak ();    System.out.println (P.getage ()); }}
 Packagecom.itstar.demo05;ImportJavax.swing.plaf.synth.SynthSeparatorUI;/** Class Description Person: * Attribute: Name Age * Method: Speak*/ Public classPerson {PrivateString name; Private intAge ; //get does not add this because there is no variable with the same name     PublicString GetName () {returnname; }    //assignment of the member variable name age     Public voidsetName (String name) { This. name = name;//add reading, assign a local variable to his member variable    }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }         Public voidSpek () {String name= "Owen"; intAge = 51; //System.out.println (this);System.out.println ("People talking" + This. Name + "..." + This. Age); }    }
 package   com.itstar.demo05;  /*   * Test class  */ public  class   Testperson { public  static  void   main (string[] args) { /span>//  Create object  person p = new   person ();  //  Call the Set method, assign a value to the member variable  p.setage (18<        Span style= "color: #000000");        P.setname ( li total 
 Packageom.itstar.demo06; Public classPerson {Private intAge ;  Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }         Public BooleanCompare (person p) {//p1.age > P2.age? Who is this? who called the P1?        return  This. Age >P.age; }}
 Packageom.itstar.demo06; Public classTestperson { Public Static voidMain (string[] args) {//test the Age comparison feature in the person classPerson P1=NewPerson (); Person P2=NewPerson (); P1.setage (20); P2.setage (15); Booleanb =P1.compare (p2);    System.out.println (b); }}

Big Data <javase + Linux Elite Training class >_day_08

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.