This and super (copy the java,c# the same)

Source: Internet
Author: User

These days to see the class in the inheritance will use this and super, here do a little summary, with you to communicate, there are errors please correct me ~

This

This is an object of its own, representing the object itself, which can be understood as: A pointer to the object itself.

The usage of this can be broadly divided into 3 types in Java:

1. Normal Direct references

This does not have to be said, this is equivalent to pointing to the current object itself.

2. The name of the member who participates in the membership is distinguished by this:

1234567891011121314151617 classPerson {    privateintage = 10;    publicPerson(){    System.out.println("初始化年龄:"+age);}    publicintGetAge(int age){        this.age = age;        returnthis.age;    }}publicclasstest1 {    publicstaticvoidmain(String[] args) {        Person Harry = newPerson();        System.out.println("Harry‘s age is "+Harry.GetAge(12));    }}       

Operation Result:

Age of initialization: 10
Harry ' s age is 12

As you can see, age here is the formal parameter of the Getage member method, and This.age is a member variable of the person class.

3. Reference constructors

Let's talk about this with super, see below.

Super

Super can be understood as a pointer to a super (parent) class object, which refers to a parent class closest to itself.

There are three ways to use super:

1. Normal Direct references

Like this, super is the parent class that points to the current object, so you can refer to the members of the parent class with Super.xxx.

2. The member variable or method in the subclass has the same name as a member variable or method in the parent class

123456789101112131415161718192021 classCountry {    String name;    voidvalue() {       name = "China";    }} classCity extends Country {    String name;    voidvalue() {    name = "Shanghai";    super.value();      //调用父类的方法    System.out.println(name);    System.out.println(super.name);    }     publicstaticvoidmain(String[] args) {       City c=newCity();       c.value();       }}

Operation Result:

Shanghai
China

As you can see, both the methods of the parent class are called and the variables of the parent class are called. If you do not call the parent class method value () and only call the parent class variable name, the parent class name value is the default value NULL.

3. Reference constructors

Super (parameter): Invokes one of the constructors in the parent class (which should be the first statement in the constructor). This (parameter): Call the constructor of another form in this class (should be the first statement in the constructor).
123456789101112131415161718192021222324252627282930313233343536 class Person {     publicstaticvoidprt(String s) {        System.out.println(s);           Person() {        prt("父类·无参数构造方法: "+"A Person.");     }//构造方法(1)        Person(String name) {        prt("父类·含一个参数的构造方法: "+"A person‘s name is " + name);     }//构造方法(2)    publicclassChinese extendsPerson {     Chinese() {        super(); // 调用父类构造方法(1)        prt("子类·调用父类”无参数构造方法“: "+"A chinese coder.");            Chinese(String name) {        super(name);// 调用父类具有相同形参的构造方法(2)        prt("子类·调用父类”含一个参数的构造方法“: "+"his name is "+ name);            Chinese(String name, int age) {        this(name);// 调用具有相同形参的构造方法(3)        prt("子类:调用子类具有相同形参的构造方法:his age is "+ age);            public staticvoidmain(String[] args) {        Chinese cn = newChinese();        cn = newChinese("codersai");        cn = newChinese("codersai"18);     }

Operation Result:

Parent class • No parameter construction method: A person.
Subclass • Call the parent class "parameterless construction method": A Chinese coder.
Parent class • Construction method with one parameter: A person's name is Codersai
Subclass • Call the Parent class "construction method with one parameter": his name is Codersai
Parent class • Construction method with one parameter: A person's name is Codersai
Subclass • Call the Parent class "construction method with one parameter": his name is Codersai
Subclass: Calling subclasses to construct methods with the same formal parameters: His-age is 18

As you can see from this example, you can use super and this to call the constructor of the parent class and other forms of construction methods in this class, respectively.

In the example Chinese class, the third constructor method calls the second construction method in this class, and the second constructor calls the parent class, so the constructor of the parent class is called first, the second in this class is called, and the third method of construction is overridden.

The similarities and differences between Super and this:

    • Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)
    • This (parameter): Calls another form of the constructor in this class (should be the first statement in the constructor)
    • Super: It refers to a member in the immediate parent class of the current object (used to access member data or functions in the parent class that is hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as super. Variable name super. member function by name (argument)
    • This: it represents the current object name (where it is easy to generate two semantics in the program, you should use this to indicate the current object, if the function's shape participates in the class member data has the same name, this should be used to indicate the member variable name)
    • The call to super () must be written on the first line of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method is implicitly called super (), and if the parent does not have this form of constructor, it will be error-free at compile time.
    • Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.
    • Super () and this () all need to be placed in the first row within the construction method.
    • Although you can call a constructor with this, you cannot call two.
    • This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will not pass.
    • This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.
    • Essentially, this is a pointer to this object, but super is a Java keyword.

This and super (copy the java,c# the same)

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.