Java Basics Learning Day Seventh--object-oriented common concept __java

Source: Internet
Author: User
Document Version Development Tools Test Platform Project Name Date author Notes
V1.0 2016.02.26 Lutianfei None
member variables and local variablesThe difference between a member variable and a local variable:
A: The position in the class is different
Member variables: In a class, method OutsideLocal variables: In in the method definitionOr on the method declaration。 B: The location in memory is different:
Member variables: in HeapMemory Local variables: in StackMemory C: Different life cycles
Member variables: With Objectis created and disappears as the object disappears: The local variables disappear as Method, with the method's call complete and the D: initialization values are different
Member variables: there are Default Initial valueLocal variables: no default initial value, you must define and assign values before you can use the. Note: The local variable name can be consistent with the name of the member variable, when used in the method, using the The principle of proximityformal parameter problems for class typesThe formal argument for a method is a class type (reference type), and what you need here is actually objects of this class
//form parameter is the basic type class Demo {public int sum (int a,int b) {return a + B;
    }//form parameter is reference type class Student {public void Show () {System.out.println ("I love Learning");
    } class Studentdemo {//If you see a method in which the formal argument is a class type (reference type), the object of that class is actually needed.
        The address of S in the main method is passed to here Student s = new Student () when public void methods (Student s) {//called);
    S.show ();
        } class Argstest {public static void main (string[] args) {//formal parameter is the basic type of Invoke demo D = new demo ();
        int result = D.sum (10,20);
        SYSTEM.OUT.PRINTLN ("Result:" +result);

        System.out.println ("--------------");
        The formal parameter is a reference type call//requirement: I want to call the method () methods in the Studentdemo class Studentdemo SD = new Studentdemo ();
        Create student Object Student s = new Student (); Sd.method (s); Give the address of S to here} 


Anonymous object Anonymous object: Is an object with No Name . is a simplified representation form of an object. Two uses of Anonymous objects
Object Invoke method only once as actual parameter pass anonymous invocation benefit
Anonymous object call complete is garbage. Can be reclaimed by the garbage collector.

Class Student {public
    void Show () {
        System.out.println ("I love Learning");
    }

Class Studentdemo {public
    void method (Student s) {
        s.show ()

}} Class Nonamedemo {public
    static void Main (string[] args) {
        //call with name
        Student s = new Student ();
        S.show ();
        S.show ();
        System.out.println ("--------------");

        Anonymous object
        //new Student ();
        Anonymous object calls method
        new Student (). Show ();
        New Student (). Show (); Here is actually recreating a new object
        System.out.println ("--------------");

        Anonymous objects are passed as actual parameters to
        studentdemo sd = new Studentdemo ();
        Anonymous Object
        sd.method (new Student ());

        Another
        new Studentdemo (). Method (New Student ());


Encapsulation

Encapsulation Overview: Refers to the hidden object's attributes and implementation Details , only to provide public access mode .

Benefits: Hide implementation Details, provide public access to improve code reusability and improve security

Encapsulation principle: Content that does not need to be provided externally is hidden. Hides properties and provides public methods to access them.

Note: Test classes typically create only objects and invoke methods. The private keyword is a privilege modifier and is a manifestation of encapsulation. Members that can be decorated with members ( member variables and member methods ) are only accessible in this class if they are decorated by private. The most common application of private is to provide the corresponding getxxx ()/setxxx () method of the member variable with private adornment () This keyword this: represents an object reference to the class in which it is located method is called by which object, this represents the object

Class Student {
    //name
    private String name;
    Ages
    private int age;

    Name get value public
    String getName () {return
        name;
    }

    The name setting value public
    void SetName (String name) {//name = "Brigitte";
        Student.name = name;
        this.name = name;

    Age Get value public
    int Getage () {return aged
        ;
    }

    Age assignment Public
    void setage (int age) {
        this.age = aged;
    }
}

Test class class
Studenttest {public
    static void Main (string[] args) {
        //Create student object
        Student s = new Student () ;

        Assigning values to member variables
        s.setname ("Brigitte");
        S.setage (a);
        Gets
        the data System.out.println (S.getname () + "---" +s.getage ());
    }


Construction Method

Constructor Action Overview to initialize data for an object

The constructor format method name is the same as the class name and has no return value type, and even void does not have a specific return value. (Can have return; This statement, which means that the method ends here)

Construction method Considerations If you do not provide a constructor method, the system will give the default parameterless construction method if you provide a constructor method, the system will no longer provide the default parameterless construction method. Must be given out by himself. (It is recommended that you always give a parameterless construction method.) construction methods can also be overloaded

Class Student {private String name;

    private int age;
    Public Student () {System.out.println ("This is a parameterless construction method");
        }//constructor overloaded format public Student (string name) {System.out.println ("This is a constructed method with a String type");
    THIS.name = name;
        Public Student (int age) {System.out.println ("This is a constructed method with an int type");
    This.age = age;
        Public Student (String Name,int age) {System.out.println ("This is a construction method with multiple parameters");
        THIS.name = name;
    This.age = age;
    public void Show () {System.out.println (name+ "---" +age); 
        } class ConstructDemo2 {public static void main (string[] args) {//create object Student s = new Student ();
        S.show ();

        System.out.println ("-------------");
        Create object 2 Student s2 = new Student ("Brigitte");
        S2.show ();

        System.out.println ("-------------");
        Create Object 3 Student s3 = new Student (27);
        S3.show (); System.out.println("-------------");
        Create Object 4 Student s4 = new Student ("Brigitte", 27);
    S4.show (); }
}


Standard code for a basic class member variable construction method
The method of constructing the parameter with parameters in the non-parametric construction method

Member Method GetXXX () setxxx ()

Ways to assign values to member variables +setxxx () Parametric construction method

/* The final version of a standard code.
            Student class: member Variable: Name,age construction Method: Parameterless, with two parameter members method: GetXXX ()/setxxx () Show (): Outputs all member variable values of the class to the member variable: a:setxxx () method B: How the method outputs member variable values: A: Obtain and then spell by GETXXX ()
    B: By calling the show () method to fix the */class Student {//name private String name;

    ages private int age;
        Construct method public Student () {} public Student (String Name,int age) {this.name = name;
    This.age = age;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    public int getage () {return age;
    public void Setage (int age) {this.age = age;
    //Output All member variable values public void show () {System.out.println (name+ "---" +age);
        }//Test class class Studenttest {public static void main (string[] args) {//mode 1 assign value to member variable/no parameter construct +setxxx () Student S1 = nEW Student ();
        S1.setname ("Brigitte");
        S1.setage (27);
        Output Value System.out.println (S1.getname () + "---" +s1.getage ());
        S1.show ();

        System.out.println ("----------------------------");
        Mode 2 assigns values to member variables Student s2 = new Student ("Liu Yi", 30);
        System.out.println (S2.getname () + "---" +s2.getage ());
    S2.show (); }
}


Student s = new Student (); What to do in memory load Student.class file into existence stack memory for s to open space in heap memory for student object to open up space to The student object's member variable is initialized by default to display initialization of the student object 's member variable assign values to the student object's member variables by constructing The Student object initialization complete , assigning the object address to the s variable


static keyword (difficult)

You can modify member variables and member methods

The Static keyword feature is loaded with the class and takes precedence over all objects that exist by the class .
This is also the condition in which we determine whether to use static keywords can be called by the class name , or through the object invocation .

Static keyword considerations in a static method There is no this keyword ( cannot reference non-static variable xxx from a static context )
Because the static is loaded as the class is loaded, this occurs as the object is created. Static objects exist first. Static methods can access only static member variables and static member methods, and non-static methods are freely accessible .

class Teacher {public int num = 10;

    public static int num2 = 20; public void Show () {System.out.println (num);//implicitly tells you that you are accessing the member variable SYSTEM.OUT.PRINTLN (this.num);

    Member Variable SYSTEM.OUT.PRINTLN (NUM2);
        The non-static variable num//system.out.println (num) cannot be referenced from the static context by public static void method () {/);

        System.out.println (NUM2);
        Non-static method function ()//function () cannot be referenced from a static context.
    Function2 (); The public void function () {} is public static void Function2 () {}} class Teacherdemo {public stat
        IC void Main (string[] args) {//create object Teacher t = new Teacher ();
        T.show ();
        System.out.println ("------------");
    T.method (); }
}

The difference between a


static variable and a member variable is the same as different
static The variable belongs to class , so it is also called the class variable member variable belongs to object , so it is also called instance Variables ( object variable ) in memory position different
static variables stored in method area are stored in the static area member variable heap Memory memory time different
static variables are loaded with the load of the class , and disappear as the class disappears the member variable with the object The is created and disappears as the object disappears calling different
static variables can be invoked through the class name or through the object invocation A member variable can only be called through object name The Main method is static public static void main (string[ ] {}
is public publicly and has the greatest access permissions because it is invoked by the JVM and requires access to be large enough to be args. Statically static, does not need to create objects, is called by the JVM, do not create objects, direct class name Access void is invoked by the JVM, do not need to return the value of the JVM main a generic name, although not a keyword, but recognized by the JVM String [] args
Eg:java maindemo Hello World Java

used to receive keyboard input

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.