The use of super

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 methodThe 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 (); Br>}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:
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 the parent class with the same formal parameter as the constructor (2) prt ("His name is:" +name); } Chinese (String name,int age) {This (name);//Call the constructor with the same formal parameter (3) prt ("He is:" +age); }  Public static void Main (string[] args) { Chinese cn=new Chinese (); Cn=new Chinese ("Kevin"); cn=new Chinese ("Kevin", 22);  

Super's usage

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.