Two ways to use Super in Java

Source: Internet
Author: User
By defining a method or member with static, it provides some convenience for us to program, and to some extent it is similar to global functions and global variables in C language. However, it is not said that with this convenience, you can use everywhere, if so, you need to seriously consider whether you are using object-oriented thinking programming, whether your own program is object-oriented.
OK, now let's discuss 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 to do this, and of course, another purpose of this is to call another constructor of the current object, which is about to be discussed immediately. If you want to refer to something of the parent class, it is not super. Since this and super have some of the same characteristics and innate relationships, we are here to discuss it, hoping to help you distinguish and master them two.
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, and you need to explicitly use the This keyword to indicate that you want to use a member in order to avoid confusion, using the method "this." Member name, and the one without this is the formal parameter. In addition, you can also use the "this". Method name to refer to a method of the current object, but this is not necessary, you can access that method directly with the method name, and the compiler will know what you are calling. The following code illustrates the above usage:
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 it's not necessary.
Setage (age);
This.print (); Br>}
public void SetName (String name) {
this.name=name;//Here you must indicate that you want to refer to the member variable
}
public void Etage (int age) {
This.age=age;
}
public void print () {
System.out.println ("name=" +name+ "ge=" +age);
This is not needed in this row because there is nothing that will cause confusion
}
public static void Main (string[] args) {
Demothis dt=new demothis ("Kevin", "22");
This code is simple, and you should be able to see it without explaining it. In the constructor you see the This.print (), you can use print () to replace it, the effect is the same. 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 repeats the Print method of the parent class, which first does its own thing, and then invokes 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 access the quilt class, you can use it like this, using "super." The name of the member in the parent class, but often you do not access the names of the members of the parent class in this way.
Constructors in constructors are a special method that is invoked automatically when an object is initialized. In the constructor, this and super also have the various uses described above, and it has a special place, please 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 (); Calling the parent class constructor (1)
PRT ("A Chinese."); /(4)
}
Chinese (String name) {
Super (name),//call constructor with the same formal parameter for the parent class (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", 22);
}
}
In this program, this and super are no longer used as "." To connect a method or member, but directly following
The appropriate parameters, so the meaning of it has changed. The Super plus parameter is used to invoke the same form in the parent class
constructors, such as 1 and 2. This parameter then invokes the constructor that currently has the same parameters, such as 3. Of course, in
The various uses of this and super in the general method are still available in the overloaded constructors of Chinese, such as 4, where you
It can be replaced 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. But this seems like a little superfluous.
Up.
Finally, written so much, if you can refer to "This usually refers to the current object, super usually refers to the parent class" This sentence is kept in mind
Heart, then this article has achieved the goal, other you will be in later programming practice slowly realize, grasps. Another about this
For the inheritance mentioned in the article, see the related 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.