Cock wire from flying to architect (object-oriented articles) -this super

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.

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.

Use super class in JAVA to refer to components of the parent class, if a class inherits from another class, our new instance of the object of this subclass when the subclass object which will have a parent object. How to reference the parent class object inside it? To refer to the use of super, super is a reference to the current object inside the parent object.

17390640-896e9542feaa67cf.png

II. Knowledge Point presentation

1、this

2、Super

3, This / super difference

III. Class documentation of the corresponding video

1、this

this is an object itself, representative of the object itself, can be understood as: a pointer to the object itself.

this usage in java can be divided into three kinds:

(1) ordinary direct reference

This would not have talked about, this is the equivalent of pointing to the current object itself.

Code Example:

public class Hello {

String s = "Hello";

public Hello(String s1) {

System.out.println("s = " + s1);

// this current object, call s property

System.out.println("1 -> this.s = " + this.s);

this.s = s1; // member variable is assigned the value of the parameter, the value of a member variable change

System.out.println("2 -> this.s = " + this.s);

}

public static void main(String[] args) {

Hello x = new Hello("HelloWorld!");

System.out.println ( "s =" + xs); // change the variable value of the membership verification

}

}

operation result:

s = HelloWorld!

1 -> this.s = Hello

2 -> this.s = HelloWorld!

s=HelloWorld!

(2) the name of the same name-shaped members involved with this distinguished:

Code Example:

class Person {

private int age = 10;

public Person(){

System.out.println ( "Initialization Age:" + age);

}

public int GetAge(int age){

this.age = age;

return this.age;

}

}

public class test1 {

public static void main(String[] args) {

Person Harry = new Person();

System.out.println("Harry's age is "+Harry.GetAge(12));

}

}       

(3) reference Constructor

Speak together on this and super, see below.

(4) Sometimes we will use some of the inner and anonymous classes, such as event handling. When this anonymous type used, this refers to the anonymous or internal class itself. Then if we want to use the methods and variables outside the class, then the class name should be added to the outer class. (If you do not understand in this case, we will have behind the corresponding knowledge to explain, can be ignored)

Code Example:

public class HelloB {

int i = 1;

public HelloB() {

Thread thread = new Thread() {

public void run() {

for (int j=0;j<20;j++) {

HelloB.this.run (); // call the method outside the class

try {

sleep(1000);

} catch (InterruptedException ie) {

}

}

}

}; // Note that there semicolon

thread.start();

}

public void run() {

System.out.println("i = " + i);

i++;

}

public static void main(String[] args) throws Exception {

new HelloB();

}

}

(5) this pass a plurality of parameters simultaneously

Code Example:

public class TestClass {

int x;

int y;

static void showtest (TestClass tc) {// instantiate an object

System.out.println(tc.x + " " + tc.y);

}

void seeit() {

showtest(this);

}

public static void main(String[] args) {

TestClass p = new TestClass();

p.x = 9;

p.y = 10;

p.seeit();

}

}

2、super

super can be understood as pointing to his own super (parent) of a class object pointer, and this superclass refers to the nearest one parent class.

There are also three super usage:

(1) ordinary direct reference

And this is similar, super equivalent refers to the current object's parent class, so that you can use super.xxx to refer to members of the parent class.

(2) sub-class member variable or method with the same name as the parent class member variable or method

Code Example:

class Country {

String name;

void value() {

name = "China";

}

}

public class City extends Country {

String name;

void value() {

name = "Shanghai";

super.value (); // call the parent's method

System.out.println(name);

System.out.println(super.name);

}

public static void main(String[] args) {

City c=new City();

c.value();

}

}

operation result:

Shanghai

China

We can see, both the parent class method called here, also called variable parent class. If the parent class method calls the value (), only the variable name to call the parent class, then the parent class name is the default value null.

(3) reference Constructor

super (parameters): one call parent class constructor (the constructor should be the first statement).

this (parameters): call another form of this class constructor (the constructor must be the first statement).

Code Example:

class Person { 

public static void prt(String s) { 

System.out.println(s); 

Person() { 

prt ( "parent-no-argument constructor:" + "A Person."); 

} // constructor (1) 

Person(String name) { 

prt ( "parent-a constructor parameter containing:" + "A person's name is" + name); 

} // constructor (2) 

public class Chinese extends Person { 

Chinese() { 

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

PRT ( "call the parent class · subclass of" no-argument constructor ":" + "A chinese coder."); 

Chinese(String name) { 

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

PRT (constructor "call the parent class · subclasses" contains a parameter ":" + "his name is" + name); 

Chinese(String name, int age) { 

this (name); // call the constructor parameter of the same (3) 

prt ( "subclasses: subclass have a constructor that calls the parameter of the same: his age is" + age); 

public static void main(String[] args) { 

Chinese cn = new Chinese(); 

cn = new Chinese("codersai"); 

cn = new Chinese("codersai", 18); 

}

3, this / super difference

(1) super (parameters): invoke a constructor of a base class (constructor should be the first statement)

(2) this (parameters): call the constructor for this class of another form (the constructor should be the first statement)

(3) super: it refers to a member of the direct parent of the current object (the parent class is used to directly access the hidden parent class data members or functions, the base class and a derived class with the same members as defined when: super. variable name super. According to a member function name (arguments)

(4) this: it represents the current object name (easy to produce the bis ambiguity in the program, this should be used to indicate the current object; participating member function if the shape data of the same name class, time required to specify the members of this 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 is not this form of the constructor, then the error will be at compile time.

(6) super (), and this () is similar except that, Super () call from the parent class constructor subclass, this () call other methods within the same class.

(7) super (), and this () constructor required in the first row.

(8) Although it is possible to call a constructor with this, but not two calls.

(9) this super and can not appear in a constructor function, because this is bound to call other constructors, other constructor must also have the presence of super statement, so in the same constructor which has the same statement, meaningless statement, the compiler will not pass.

(10) this () and super () refers to the object are, therefore, not be used in a static environment. Comprising: static variables, static methods, static block.

17390640-fc30fe9d2418554b.png

(11) In essence, this is a pointer to this object, but super is a Java keyword.

Guess you like

Origin blog.csdn.net/weixin_33912453/article/details/90784906