Java this and super usage

Source: Internet
Author: User

In Java, this usually refers to the current object, and super refers to the parent class. When you want to reference something of the current object, such as a method of the current object or a member of the current object, you can use this to achieve this purpose. Of course, another purpose of this is to call another constructor of the current object, which will be discussed soon. If you want to reference something of the parent class, it is not super. Since this has some similar characteristics and inherent relationships with super, we will discuss them here, hoping to help you differentiate and master them.

In the general method The most common situation is that a parameter name in your method has the same name as a member of the current object. In this case, to avoid confusion, you need to explicitly use the this keyword to specify that you want to use a member. The usage is "This. member name. In addition, you can also use "this. "method name" to reference a method of the current object, but this is not necessary at this time. You can directly use the method name to access that method, the compiler will know which one you want to call. The following Code Demonstrate the above usage: public class demothis {
Private string name;
Private int age;
Demothis (string name, int age ){
Setname (name); // you can add this to call the method, like this: This. setname (name); but this is not required.
Setage (AGE );
This. Print ();
}
Public void setname (string name ){
This. Name = Name; // you must specify the member variable to reference.
}
Public void setage (INT age ){
This. Age = age;
}
Public void print (){
System. Out. println ("name =" + name + "age =" + age); // This does not need to be used in this line, because there is no obfuscation
}
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 can use this. Print () instead of print. Modify this Program To demonstrate super usage. 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 re-defined print method overwrites the print method of the parent class. It first does some of its own work and then calls the overwritten method of the parent class. The output shows this: demosuper:
Name = Kevin age = 22. In addition, if the members of the parent class can access the quilt class, you can use it like this and use "super. in the parent class, but you often do not access the Member names in the parent class in this way. In the constructor Constructor is a special method that is automatically called during object initialization. In the constructor, this and super also have various usage methods described above, and they also have special features. Please refer to the example below: 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 the parent class Constructor (1)
PRT ("A Chinese."); // (4)
}
Chinese (string name ){
Super (name); // call the constructor with the same parameters in the parent class (2)
PRT ("His name is:" + name );
}
Chinese (string name, int age ){
This (name); // call a constructor with the same parameters currently (3)
PRT ("his age is:" + age );
}
Public static void main (string [] ARGs ){
Chinese Cn = new Chinese ();
CN = new Chinese ("Kevin ");
CN = new Chinese ("Kevin", 22 );
}
} In this program, this and super no longer use ". "connects a method or member, but directly follows the appropriate parameter, so its meaning changes. Super is used to call constructors with the same form in the parent class, such as 1 and 2. If this is followed by a parameter, the constructor with the same parameter is called, for example, 3. Of course, in each overload constructor in Chinese, the usage of this and super in general methods can still be used. For example, you can replace it with "This. PRT (because it inherits the method in the parent class) or "super. PRT (because it is a method in the parent class and can be accessed by the quilt class), it can still run correctly. However, it seems a bit fascinating. At last, I wrote so much. If you keep in mind the phrase "this usually refers to the current object, super usually refers to the parent class", then this article achieves the goal, others you will learn and master in future programming practices. For the inheritance mentioned in this article, refer to the relevant Java tutorial.
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.