Read "Effective java-17th" problem solving and sharing

Source: Internet
Author: User

Issue background

The recent 2 days are ready to re-read "Effective Java", found that these classic books are really look over and over again feelings. The process of learning is also becoming more and more aware of a repetitive process. This encounter problem is in the 17th article saw, looked for a long time did not understand the visual code. The content of article 17th is either designed for inheritance, provides documentation, or prohibits inheritance . There is an example in which a constructor must never invoke a method code that can be overwritten as follows :

Parent Class Code

Package com.sitech.test;/** * Effect of Java  * @author LIAOWP * */public class Super {        public Super () {        overrideme ();    }        public void Overrideme () {    }}    

Sub-class Code

Package com.sitech.test;import java.util.date;/** * Effect of Java * @author LIAOWP * */public class Sub Extends
     
       super{    Private final
       date date;        Sub () {        date =new date ();    }        @Override public    void Overrideme () {System.out.println ("time" +date);} public static void Main ( String[] args) {Sub Sub = new Sub (); Sub.overrideme ();}}       
      

Output Result:

You may expect this program to print out the date two times, but it prints null for the first time because the Overrideme method is called by the Super constructor, and the constructor sub has no chance to initialize the date field. Note that the final domain observed by this program is in 2 different states. Also note that if Overrideme has called any of the methods in date, the call throws a NullPointerException exception ( original ) when the Super Converter calls Overrideme .

questions raised  

See a lot of people see the above code will know the result, but I was stuck in the then why is the parent constructor method call Overrideme a subclass ? The moment was lost, and there was no understanding. The brain can't turn, can only check the information, but do not know how to search. So I can only take a little bit of a toss. So first add the code to print:

Problem solving

It is true that the Overrideme method of the called subclass is still not well understood. Only debug, before the debug I again in the Overrideme method add a This, I want to see who this represents.

After debug you can see that this refers to the sub,date is empty, so that you know that the parent class is called in the constructor of the overrideme is there, so we also understand why the parent class is called the method of the subclass. The subclass is instantiated in main, and this is the subclass, so the parent class calls Overrideme in the constructor method, which is the overrideme of the subclass. Perfect

Problem summary and sharing

People sometimes prone to thinking stereotypes, resulting in some people can easily see the problem of their own have been ignorant in the inside out, the problem for me is such a amount, I did not understand how at that time, fixed in a place, asked a friend asked the tutor, an instant to think clearly in the past their own verification, the problem is to find the answer. Although fixed in that is painful, but find the answer to the question is happy.

I've been reading a book recently and I've seen a very good code share for everyone. We often see the string reversal of the surface of the question, there are many ways of implementation, I see the better way to share it, this way also feel with me above that thinking problem a bit similar feeling, personal feeling encounter string reversal of the problem, many will think of loops or something. The method was unexpected. The code is as follows:

public static string reverse (string originstr) {            if (originstr = = NULL | | originstr.length () <= 1)                 Retu RN Originstr;            return reverse (originstr.substring (1)) + originstr.charat (0);//intercept the front to the back to go}  

Read "Effective java-17th" problem solving and sharing

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.