Keyword (1): super and this: super

Source: Internet
Author: User

Keyword (1): super and this: super

I have been learning Java for a long time. I have read many books and blogs, and I am also eager to write my own learning experience.

This is also a newbie: keyword in Java.

Taking the keyword as the first blog post is also considered again and again: 1. My Foundation is also weak. 2. Step 3 from step 3. Some blog posts are also flawed. Not much nonsense:

Super usage: 1. Call the constructor of the parent class:
class MyDate extends Date {        static public MyDate valueOf() {        return new MyDate();    }        static public MyDate valueOf(long time) {        return new MyDate();    }        private MyDate() {        super();    }        private MyDate(long time) {        super(time);    }}

You can call the constructor of the parent class in the subclass to pass response parameters. This parameter is used for initial identification (ps: only used in the subclass constructor ). So the question is: if the parent class constructor is private, can the subclass be called by super?

As we all know, the field, construc, and method modified by private are not allowed to be accessed by the outside world, so super should not be called.

IDE (Eclipse) is not allowed directly

Compilation Error

2. Call the parent class method or a member of the parent class:
class MyDate extends Date {        static public MyDate valueOf() {        return new MyDate();    }        private MyDate() {        super.getTime();    }        public void doAction() {        super.getTime();    }}

As shown above, the method for calling the parent class is relatively free: The subclass method and the subclass constructor can all be called. So the question is: What if I call the parent class method first and then call the parent class constructor?

It is easy to think that this is not allowed. The parent class has not yet constructed how to call the parent class method...

IDE (Eclipse) is not allowed directly

When an error is reported during compilation, the following message is displayed:The super call must be the first statement in the constructor.

Of course, you can also try other cases. Is super's call required to be the first statement?

3. Generic applications:

Many blog posts list the above two types, but the wildcard also has the super keyword, so I should count it as a usage.

The super keyword declares the lower bound of the type, indicating that the parameterized type may be the specified type, or the parent type of the type until the Object

The extends keyword declares the upper bound of the type, indicating that the parameterized type may be the specified type or a subclass of this type.

It looks a bit round, but a simple example shows why super is needed:

public class MainTest {    static public void main(String[] args) {    }        static private <T extends Comparable<? super T>> T min(T a, T b) {        if (a == null || b == null) {            return null;        }        if (a.compareTo(b) < 0) {            return a;        }        return b;    }}class SuperTest implements Comparable<SuperTest> {    @Override    public int compareTo(SuperTest o) {        return 0;    }}class SubTest extends SuperTest {}

The code above indicates that min is used to find the small and medium-sized ones of the two T types (which is intended to be implemented in this way ). When the generic type is used, the parameter type is T, and T must be a Comparable subclass or implement the Comparable interface. Considering the wide applicability, T can also be used if a parent class implements the Comparable interface.

Usage of this: 1. Call the constructor of the object itself
class MyDate extends Date {        static public MyDate valueOf() {        return new MyDate();    }    private MyDate() {        this(0);    }    private MyDate(long time) {    }}

Similar to the usage of super, it must be the first statement in the constructor, but there is another problem: if this is called each other in the constructor, that is, in private MyDate (long time ){This ();}. What will happen?

The error is obvious: recursive call is prohibited.

2. Call the method of an object or a member of the object.

 

class MyDate{        private long myDate;    static public MyDate valueOf() {        return new MyDate();    }    public void print() {        System.out.println(this.getMyDate());    }    public long getMyDate() {        return this.myDate;    }}

 

Misunderstanding of this & super:

This calls the object itself, and super calls the parent class of the object. This understanding is still inaccurate.

Lou pig thinks it is relatively good to understand:This is the reference of the object itself, and super is just a keyword with prompting significance. Note: this is a class object, and super is not a parent class object.

 

Import java. util. *; class MyDate extends Date {static public void main (String [] args) {MyDate. valueOf (). print ();} static public MyDate valueOf () {return new MyDate ();} private MyDate () {} public void print () {System. out. println (this); System. out. println (this. getClass (); System. out. println (super); // compilation fails through System. out. println (super. getClass ());}}

 

If the above Code is added with System. out. println (super); Compilation fails.

Compilation error. The Compiler tries to tell us that super is not used in this way.

And System. out. println (this); this is allowed.

Another method is to determine whether an object is an instance of a class by using the keyword instanceof ):

Import java. util. *; class MyDate extends Date {static public void main (String [] args) {MyDate. valueOf (). print ();} static public MyDate valueOf () {return new MyDate ();} private MyDate () {} public void print () {if (this instanceof MyDate) {System. out. println ("this is object");} if (super instanceof Date) {// compilation fails through System. out. println ("super is object ");}}}

Compilation Error

With such a long-winded mind, on the one hand, I was too short to express my thoughts. On the other hand, I wrote a blog for the first time, but I was quite nervous about the layout, wording, effect, and so on.

If you have any questions, please let me know...

 

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.