The difference between this and super in Android

Source: Internet
Author: User

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 Setage (int.) {
This.age=age;
}
Public void Print () {
System.out.println ("name=" +name+ "age=" +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", "a");
}
}
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 (a);
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.
in the constructor 
A constructor is a special method that is 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:
class person{
Public static 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 Parent class constructor (1)
prt ("A Chinese."); /(4)
}
Chinese (String name) {
super (name);//The constructor that invokes the parent class with the same parameters (2)
prt ("His name is:" +name);
}
Chinese (String name,int age) {
This (name);//Call the constructor that currently has the same formal parameter (3)
prt ("His age is:" +age);
}
Public static void Main (string[] args) {
Chinese cn=new Chinese ();
cn=new Chinese ("Kevin");
cn=new Chinese ("Kevin");
}
}
In this program, this and super are no longer used as before. Connect a method or member, but follow the appropriate parameters directly thereafter, so the meaning of it changes. Super is used to invoke constructors that have the same form in the parent class, such as 1 and 2. This is followed by a constructor that currently has the same parameters, such as 3. Of course, in the various overloaded constructors of Chinese, this and super are still available in various usages of the general method, such as 4 where you can replace it with "this.prt" (because it inherits that method from the parent class) or "Super.prt" (because it is a method in the parent class and can be accessed by the class), it works correctly. But it seems to be a little superfluous flavor.
Finally, write so much, if you can "this usually refers to the current object, super usually refers to the parent class" This sentence to keep in mind, then this article will achieve the goal, other you will be in the future programming practice slowly experience, master. Also for the inheritance mentioned in this article, see the related Java tutorials


Original address: http://blog.163.com/[email protected]/blog/static/1717240672012620111028892/

The difference between this and super in Android

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.