Object-this keyword

this this is a reference type, this is a keyword in java, is a reference type, in which the heap (heap), each object has a java this point to themselves. this represents the current object references,

His role is to:

      1. distinguish member variables.

      2. The distinction between local variables

      3. Call member variables or local variables

      4. Call the members of the methods and constructors

note:

      Maybe not in the static keyword static method internal standard line

Each time a behavior, certainly there will be a subject of this behavior occurs. In the programming language inside every method defined in, this will have a key, the key to this there are not defined by the determined, but the implementation of the decisions by whom. This is the key to this judgment.

1, thia Overview  

    For example, eat this method it is defined by God, all of them performed in the world. This behavior occurs when a meal, the body is in this person to eat, that is, to eat someone to perform this action. Sometimes we need a behavior (method), the can clearly know that this behavior is, precisely, who performed what I want to know who is eating.

function eatSomthing() { this.eat }

In the programming language inside every method defined in, this will have a key, the key to this there are not defined by the determined, but the implementation of the decisions by whom. This is the key to this judgment.

2, this principle   

For example: This person is Joe Smith, then eat the body is "Joe Smith"

Joe Smith .eat (); // this dinner where this is Joe Smith this object.

Is judged (.) In front of who is targeted, then this this is who. However, some methods are performed directly in front of no points, no more execution objects, then this key Who is it ?

Like alert method, direct execution, that alert method where this Who is it? Anything that can be run directly this method, are global method (method called global function), perform a global approach, as the alert is equivalent to the previous omitted out window., that is to say alert () is equivalent to window.alert (), so that the direct method of operation in this key, be sure it is a window.

* Note: this method in the representation of that object is, the implementation of the decision by whom. And there is no relationship defined.

Supplementary: this represents the object is "context" This method activities. The so-called "context" that is, the code represents the range of current activities, current activities (execution code) mean that occur on that object.

Eat this activity is occurring on the object seating, the seating is below the meal. Find a div element in the body go, look for the body is the context of this event. That this this, this is the way to run a range of activities.

The method in this context that the current method of operation, but is not limited to this context (that is, the difference between the overall and individual).

 

this: in vivo method can only be used for this keyword method, is a reference to "that object to invoke a method" of. When an object is created, Java Virtual Machine (JVM) will be assigned a reference to the object itself pointer that name is this. Thus, the this class can only be used in non-static methods, static methods and static code block must not occur this.

 

super: super similar keywords and this effect, the member variables and member methods of the parent class is hidden becomes visible, or used to reference member variables and member methods are hidden in the parent class.

 

1. With super and this call member variables and methods

 

Sometimes encounter subclass member variable or method with the same name as the parent class member variable or method. Because the member variable or method name of a subclass of high priority, the same name as a member variable or method subclass hides the member variable or method of the parent class, if you want to use the parent class member variable or method, you need to use super. 

 

class Country {

       String name;

       voidvalue () {

              name = "China";

       }

}

 

class City extends Country {

       String name;

       void value() {

              name = "Hefei";

              super.value();

              // When this method is not called, super.name returns a null value member variable of the parent class

              System.out.println(name);

              System.out.println(super.name);

       }

       public static void main(String[] args) {

              City c=new City();

              c.value();

       }

}

 

Results: Hefei

        China

 

To name a reference member variables and methods of the parent class value subclass (), using the super, super.name and super.value () in the code. When you do not call super.value (), super.name Returns the parent class member variables default value null, this method is called, super.value () method of the member variable name assigned to China, reuse call the parent class members super.name value of the variable.

 

Also, note that super.name call is the value of a member variable of the parent class.

 

class Country {

       String name="xianfan";

       String value(String name) {

              name = "China";

              return name;

       }

}

 

class City extends Country {

       String name;

       String value(String name) {

              name= "Hefei";

              super.value ( "failed");

              // When this method is not called, super.name returns a null value member variable of the parent class

              System.out.println(name);

              System.out.println(super.name);

              return name;

       }

 

       publicstatic void main(String[] args) {

              City c=new City();

              c.value("成功");

       }

}

    Results: Hefei

            xianfan

 

Then, the value returns the value of xianfan super.name parent class member variables, but this time super.value () method is ineffective.

 

2. With super and this constructor call

 

class Person {

       publicstatic void prt(String s) {

              System.out.println(s);

       }

 

       Person() {

              prt("A Person.");

       }

 

       Person(String name) {

              prt("A person name is:" + name);

       }

}

 

public class Chinese extends Person {

       Chinese() {

              super (); // call the parent class constructor (1)

              prt("A chinese.");//(4)

       }

 

       Chinese(String name) {

              super (name); // call the parent class constructor having the same parameter (2)

              prt("his name is:" + name);

       }

 

       Chinese(String name, int age) {

              this (name); // constructor calls this class have the same parameter (3)

              prt("his age is:" + age);

       }

 

       publicstatic void main(String[] args) {

              Chinese cn = new Chinese();

              cn = new Chinese("kevin");

              cn = new Chinese("kevin", 22);

       }

}

 

The result is: A Person.

        A chinese.

        A person name is:kevin

        his name is:kevin

        Aperson name is:kevin

        his name is:kevin

        his age is:22

 

During this program, this is no longer a super and, as with as before. "" One way or the connection member, but later to keep up with the appropriate parameters, so it will have a direct sense of change. After adding super parameter it is used to call the parent class constructor parameter having the same form as (1) and (2) at. After this addition the parameter is called the current class constructor parameter having the same form, as indicated at (3). Of course, in each of the Chinese constructor overload, and this super general usage can still be used, such as (4) at, you can replace it as a "this.prt" (because it inherits the methods of the parent class ) or "super.prt" (because it is the parent class and method quilts can access), it can still function correctly. But this seems somewhat superfluous flavor.

 

3. Constructor of a subclass if you want to refer super , it must be super in the first line of approach. 

 

class Base {

       Base() {

              System.out.println("Base");

       }

}

 

public class Checket extends Base {

       Checked () {

              super (); // call the parent class constructor, be sure to put the method's first statement

              System.out.println("Checket");

       }

       publicstatic void main(String argv[]) {

              Checked checked c = new ();

       }

}

 

If you want to call the superclass constructor with super way, but not on the first line, then the statement before the super, must be completed in order to satisfy the statement they want some action, but it is used to call the parent class constructor super method, so changes made before they all go back up, that became the constructor of the parent class.

 

to sum up:

 

1) super (parameters): invoke a constructor a parent class, method of construction should be the first statement.

2) this (parameters): call another form of the present method of configuration class constructor should be the first statement.

 

3) super: direct reference to members of the current parent object, the method used to access the member variables or direct parent class is hidden. When using the parent class and subclass of the same members, such as: super super member variable name or method name (argument)...

 

4) this: name representative of the current object, is easy to produce the bis ambiguity in the program, this should be used to indicate the current object. If a formal method of participation in the class member variables of the same name, this time to indicate the need to use this member variable name.

 

5) call super () must be written in the first line of the subclass constructor or the compiler does not pass. The first statement in the constructor of each subclass are implicitly calls super (), if the parent class constructor is not in this form, it would be an error at compile time.

 

6) this () and super () can not appear inside a constructor, because this is bound to call other constructors, other construction methods must also exist super statement, in which the same have the same constructor statement on lost the meaning of the statement, the compiler will not pass.

 

7) this and super both refers to the object, it can not be used in a static method.

super Overview

 

this is similar to the super keyword, this is used to indicate the current instance of the class, super class is used to refer to the parent. 

It may be used in super subclasses by dot (.) to obtain the member variables and methods of the parent class. 

also be used in super subclass subclass, Java can automatically traced to the upper class. 

Parent behavior is called, it is the same as if the act of this kind of behavior, and do not have to call the behavior occurs in the parent class, it can automatically traced back to the upper class.

 

super key functions

Call the parent class variables declared private. 

Point to take a method already covered. 

As a method name indicates the parent class constructor.

 

Call the hidden variables and methods covered

  

 

 

operation result: 

Animals can move 
Dogs can walk and run 
Please remember: Animals are human’s good friends

 

move () method can also be defined in some ancestor class, such as the parent of the parent class, Java retroactively, would have been looking up, until you find the process so far.

 

By hiding variables super call the parent class, you must declare getter method in the parent class, because the data members declared as private subclass is not visible.

 

Learn java to recommend a learning exchange group, there are a lot of learning materials, above all, two hundred twenty, one hundred forty-two the middle and, finally, nine hundred and six

 

Call the constructor of the parent class 

In many cases, the default constructor to initialize the parent class object. Of course, also be used to display the super constructor call the parent class. 

 

 

The result:  I am a cute puppy, my name called Flower, I'm 3 years old 

Note: both super () or this (), must be placed on the first line of the constructor. 

Notably: calling another constructor in the constructor, invoke actions must be placed most of the starting position. 
You can not call the constructor in any method other than a constructor. 

Within a constructor method call only a constructor. 

If in a constructor method, neither call super () does not call the this (), the compiler automatically inserts a call to the constructor of the superclass, but with no parameters. 

Finally, note the difference with this super: Quote super is not an object can not be assigned to another object variable super, it's just a special keyword instructs the compiler to call the parent class method.

Guess you like

Origin www.cnblogs.com/JiXianSen/p/12131570.html