this keyword + super keyword

A keyword .this
1. Example I:

(1) requirements: a description of a Java class animal;

(2) Example:

class Animal {
    String name; // member variable
    String Color;
    public Animal (n-String, String C) {
        = n-name;
        Color = C;
    }
    public void eAT () {
        String name = "mouse"; local variables //
        System.out.println (name + "eating ......");
    }
}
 
class Demo1 {
    public static void main (String [] args) {
        Animal Animal dog new new = ( "dog", "white"); // this case the presence of two name data in memory
        Animal cat = new Animal ( "cat", "black");
        cAT .eat ();
    }
}

 

Problems:

When we have a new animal cat, let the cat eat to call the method, the result is shown in rats to eat;

That is when there is a member variable of the same name and local variables within the method of access is a local variable, it is because Java is taken of the principle of proximity access, to solve this problem you need to use this keyword;

.this keyword action:

(1) If the member variable of the same name and local variables exist, the default data access method within the local variables, this keyword specifies the data can be accessed by members of the variable;

(2) a configuration in function can call another constructor to initialize the object;

matter 5.this keyword to note:

(1) the presence of a member variable of the same name and local variables within the method of access is a local variable (Java to adopt a "nearby mechanism principles "of access);

; (2) the case if accessing a variable in a method, the variables exist only member variable, then the Java compiler will add this keyword in front of the variable

6.this key word matters call other constructors to note:

when (1) this keyword call other constructors, this keyword must be located in the constructor of the first statement;

(2) the this keyword can not be in the constructor call each other's situation appears, because it is an infinite loop;
two .super keyword
represents the parent class space 1. super keyword references

2. super key role in:

(1) the existence of child-parent class members with the same name (including variables and methods), in subclasses default is a member of a subclass of access, members can access the super keyword specifies the parent class ;

(2) create a sub-class object, the default will first call the parent class constructor without parameters, it is possible to specify the parent class constructor calls the super keyword.
(1) If the parent class constructor call is not specified in the constructor of a subclass, then the java compiler will add super () statement in the subclass constructor.
When (2) super keyword to call the parent class constructor, the statement must be the first statement if the sub-class constructor.
(3) super keyword and this can not occur at the same time calling other constructors in the same constructor. Because the first two statements require a statement.
(1) on behalf of inconsistent things.
① super keyword represents the reference to the parent class space. (And do not represent the object, but represents an object in a piece of memory only)
② the this keyword represents the object belongs to the caller function.
(2) the use of the premise inconsistent.
① super keyword must have to use inheritance.
② this keyword does not require the presence of inheritance can also be used.
(3) the difference between calling the constructor:
① Super keyword is calling the constructor of the parent class.
② this keyword is to call this class constructor.

3. super key issues call the parent class constructor to note:

Note: the two keywords can not appear together in the same constructor to call other constructors, which can still write this.num = num. Not to say that can not appear this.

4. super difference between this keyword and the keyword:
Example:

class Father {
    int X =. 1;
 
    Father () {
        System.out.println ( "This is the parent class's constructor with no arguments");
    }
 
    Father (int X) {
        in this.x = X;
        System.out.println ( "this is the parent class has a constructor parameter");
    }
 
    Speak void () {
        System.out.println ( "I is the parent class");
    }
}
 
class the extends Father Son {
    int Y =. 1;
    Son () {
        System.out.println ( "This is not a subclass of configuration parameters method ");
    }
 
    son (int Y) {
        this.y from Y = X +;
        System.out.println (" this is a configuration parameter of the subclass band ");
    }
 
    void RUN () {
        super.speak (); access to the parent class function //
        System.out.println ( "I subclass");
    }
}
 
class Demo1 {
    public static void main (String [] args) {
        son new new son = S (. 3);
        the System.out. the println (SY); //. 4
    }
}

mainly in the super keyword subclass methods for parent object class to sub-objects; access property, and functions of the parent class constructor.

Guess you like

Origin www.cnblogs.com/sy130908/p/11429286.html