Usage of this and super in Java

Source: Internet
Author: User

By defining a method or member with static, it provides some convenience for our programming, which in some way can be said to be similar to global and global variables in the C language. But it's not that you can use it anywhere, and if you do, you need to think about whether you're programming with object-oriented thinking and whether your program is object-oriented.
Okay, now we're going to talk about the meaning and usage of the two keywords This&super.
In Java, this usually refers to the current object, and Super refers to the parent class. When you want to refer to something of the current object, such as a method of the current object, or a member of the current object, you can use this for this purpose, and, of course, another use of this is to invoke another constructor of the current object, which will be discussed immediately. If you want to quote something from the parent class, it's not super. Since this and the super have so many similar traits and innate relationships, we're here to discuss it, hoping to help you differentiate and master two of them.
In the general method
The most common scenario is that a parameter name in your method has the same name as a member of the current object, so you need to explicitly use the This keyword to indicate that you want to use a member, using the "this. Member name" instead of the one that is the formal parameter. In addition, you can use the "this. Method name" to refer to a method of the current object, but this is not necessary, you can directly use the method name to access the method, the compiler will know that you want to call the one. The following code demonstrates the use of the above:
public class demothis{
private String name;
private int age;
Demothis (String Name,int age) {
SetName (name);
You can add this to invoke the method, like this: This.setname (name); but that's not necessary.
Setage (age);
This.print (); }
public void SetName (String name) {
this.name=name;//must indicate here that you want to refer to the member variable
}
public void EtAge (int.) {
This.age=age;
}
public void print () {
System.out.println ("name=" +name+ "ge=" +age);
This is not required in this line because there is nothing that can cause confusion
}
public static void Main (string[] args) {
Demothis dt=new demothis ("Kevin", "22");
This code is very simple and you should be able to understand it without explaining it. In the constructor you see Using This.print (), you can completely replace it with print (), which works the same way. Here we modify this program to demonstrate the use of super.
Class person{
public int C;
private String name;
private int age;
protected void SetName (String name) {
This.name=name;
}
protected void Setage (int age) {
This.age=age;
}
protected void print () {
System.out.println ("name=" +name+ "age=" +age);
}
}
public class Demosuper extends person{
public void print () {
System.out.println ("Demosuper:");
Super.print ();
}
public static void Main (string[] args) {
Demosuper ds=new demosuper ();
Ds.setname ("Kevin");
Ds.setage (22);
Ds.print ();
}
}
In Demosuper, the redefined Print method overridden The Print method of the parent class, first doing something of its own, and then invoking the overridden method of the parent class. The output illustrates this point:
Demosuper:
Name=kevin age=22

The use of this method is more commonly used. In addition, if a member of the parent class can be accessed by a class, you can use it like this, using the "super. Member name in the parent class" way, but often you do not have access to the member names in the parent class.
Constructors are a special method in constructors that are called automatically when an object is initialized. In the constructor, this and super also have a variety of ways to use the above, and it has a special place, see the following example:

1234567891011121314151617181920212223242526272829303132333435363738 classPerson {    publicstaticvoidprt(String s) {        System.out.println(s);    }    Person() {        prt("A Person.");    }    Person(String name) {        prt("A person name is:" + name);    }}publicclassChinese extendsPerson {    Chinese() {        super(); // 调用父类构造函数(1)        prt("A chinese.");// (4)    }    Chinese(String name) {        super(name);// 调用父类具有相同形参的构造函数(2)        prt("his name is:"+ name);    }    Chinese(String name, intage) {        this(name);// 调用当前具有相同形参的构造函数(3)        prt("his age is:" + age);    }    publicstaticvoidmain(String[] args) {        Chinese cn = newChinese();        cn = newChinese("kevin");        cn = newChinese("kevin"22);    }}

  

In this program, this and super are no longer used as before. Connect a method or member, and then directly follow the
The proper parameters, so the meaning of it changes. The super parameter is used to invoke the same form in the parent class.
constructors, such as 1 and 2. This is followed by a constructor that currently has the same parameters, such as 3. Of course, in
In each of the overloaded constructors of Chinese, this and super can still be used for various uses in the general method, such as 4, where you
It can be replaced with "this.prt" (because it inherits that method in the parent class) or "Super.prt" (because it
is a method in the parent class and can be accessed by the class), it can still run correctly. But it seems to be a little superfluous.
The
Finally, written so much, if you can refer to the "This usually refers to the current object, super usually refers to the parent class" This sentence is remembered in
Heart, then this article will achieve the purpose, other you will be in the future programming practice slowly experience, master. Also on this
The inheritance mentioned in this article, see the related Java tutorials


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/yihuei123/archive/2007/06/04/1637893.aspx

Usage of this and super in Java

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.