Think in Java constructor _ Inherit _super () _android_dialog () _ Why some classes do not have constructors themselves to understand

Source: Internet
Author: User
<span id="Label3"></p> <ol> <li><li>The first is because I saw the long Zhang Teacher's tutorial <ol> <li>There's a very intuitive representation of polymorphism in it: the different constructors</li> <li>There is also a call relationship between the parent class and the constructor of the child class</li> </ol></li></li> <li>Specifically: subclasses inherit the parent class, and the child class object is created during the <ol> <li><li>First the new subclass (no Argument) process has been said before (there is no Inheritance)</li></li> <li>There is a case of inheritance,<span style="color: #ff0000;">the <span style="color: #ff0000;">first thing to do is to execute the default parameterless constructor that calls the subclass, and execute the first line of the statement inside: the default implicit constructor for the parent class is implicitly called "i think this is wrong, think it should be the first load of the parent class, but how to know that the parent class is called the default parameterless constructor?" If the life of a subclass is another constructor, it should be a subclass construct that calls the constructor of the parent class first</span> .</span>(provided that the parent class currently has only one default parent class without a parameter constructor, it can be written out or not.) If there are other constructor functions in the parent class, the parameterless constructor must be written Out.<pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #0000ff;">extends</span> <span style="color: #000000;">father{ </span>public<span style="color: #0000ff;"></span><span style="color: #0000ff;">static</span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> main (string[] Args) { children Children </span>=<span style="color: #0000ff;">new</span> <span style="color: #000000;"> children (); }}</span> <span style="color: #0000ff;">class</span> <span style="color: #000000;">father{ }<br>Execution has no error, but how do you see how the subclass is constructed by calling the default parameterless constructor of the parent class?</span></pre></pre><p><p></p></p> <ol> <li><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Children<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main (string[] Args) {children children</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Children (); }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* If the following statement is executed, the default parameterless constructor of the parent class is called</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Father () {System.out.println (</span></span>"Father constructor"<span style="color: #000000;"><span style="color: #000000;">); The result of The}}//execution is that the Father constructor proves that the subclass new object did call the default parameterless constructor of the parent class, but who called the parent Class's constructor, and the new subclass object calls the Subclass's default parameterless constructor (because The new children () does not pass the parameter , and then the constructor of the parent class is called by the default constructor of the Subclass)</span></span></pre><p><p></p></p><p><p></p></p> <ol> <li><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">gouzao;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Children<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">children () {System.out.println (</span></span>"chilren constructor"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main (string[] Args) {children children</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Children (); }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* If the following statement is executed, the default parameterless constructor of the parent class is called</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Father () {System.out.println (</span></span>"Father constructor"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* The execution result is: * Father Constructor Chilren constructor function</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;">the <span style="color: #000000;">}//description executes the constructor of the parent class before executing the constructor of the child class</span> .</span></pre><p><p><span style="color: #ff0000;">Does yiwen:1-call the constructor of the parent class to create an object of the parent class?</span><br><span style="color: #ff0000;">2.java virtual Machine How does the load mechanism of this section look like?</span></p></p></li> <li><li><strong><span style="color: #000000;">of course, the parent class can have other parameter constructors, then the subclass call is still the default parameterless constructor of the <strong><span>parent class <strong><span></span></strong> </span></strong></span></strong></li></li> </ol></li> <li><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">gouzao;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Children<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">children () {System.out.println (</span></span>"chilren constructor"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main (string[] Args) {children children</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Children (); }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* If the following statement is executed, the default parameterless constructor of the parent class is called</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Father () {System.out.println (</span></span>"Father constructor"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* of course, the parent class can write other arguments with the constructor, then the subclass calls the default parameterless constructor *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">x</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Father (String x) {System.out.println (x); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* The execution result is: * Father Constructor Chilren constructor function</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span></pre><p><p></p></p></li> <li>In addition, we know that the default parameterless constructor is not writable, so we try to hide the non-parametric constructs of the parent class:<pre><pre>Compiler Error Found: implicit parent class constructor Father () is not defined (I.E. not explicitly written out) and must be explicitly called by another Fred Constructor.<br><br>Popular: Zhang San into a gang, gangs will default to him a default teacher called John doe, of course, This teacher is still good, this teacher can come out also can not come out, do not come out, Zhang San can also find him by Default. of course, One day, there is a person called John Doe (to become a new John Doe master, before the original John Doe master), this time Zhang San the original John Doe master if there is, Zhang San if there is nothing to understand or to ask the original John Doe master, will not ask the new also called John Doe of the Guy. But when the original John Doe Master is not at the time and the gang only one can give Zhang San to help people, when Zhang San need help, The default system will default to the original Reese to find out, now, the original Li four is because it is not the default, Zhang San sometimes urgent to find his help he is not, At this time, the default system of gangs is to find the original John Doe master, but found: hey, not a new John Doe Master is always in your Zhang San side, Zhang San you to ask, I do not find you, ready-made you do not need, you also want me to find, you tease me! "so, Zhang San can also at any time to the new John Doe master to ask for help, of course, there is a price, such as the new John Doe master requires every help must give a cigarette (that is, the father of the parameters of the parameter constructor), Zhang San no way, Because the original John Doe Master is not (no explicit Definition) plus there is an emergency in addition to the gang system does not give him the original John Doe master (subclass to create the object, to invoke the constructor of the parent class), so, brought a cigarette to ask the new John Doe master to Help. That is to say, the constructor of the subclass is called when the parameterless constructor of the calling parent class discovers that the parameterless constructor of the parent class hides (without explicit Definition) the method of calling the other constructors of the parent class through Super (arguments) in the corresponding constructor of the subclass, as if in a gang, If Zhang San has a cigarette he can go to the new John Doe master (because The new John Doe is willing to help with a cigarette), of course, if Zhang San has two cigarettes, he can also ask the gang for two cigarettes to help the person (of course, this person to explicitly exist, not stays, that the constructor is explicitly defined).</pre></pre><pre><pre>Public<span> Children ()</span></pre></pre><pre><pre><span>Implicit Super constructor Father () is Undefined. Must explicitly invoke another constructor</span></pre></pre><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">gouzao;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Children<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">children () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Implicit Super constructor Father () is Undefined. Must explicitly invoke another constructor</span></span>System.out.println ("chilren constructor"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main (string[] Args) {children children</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Children (); }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Hide the non-parametric structure of the parent class and find the compiler error</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* Public Father () {* * * System.out.println ("Father constructor");} </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* of course, the parent class can write other arguments with the constructor, then the subclass calls the default parameterless constructor * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">x</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Father (String x) {System.out.println (x); }}</span></span></pre><p><p></p></p></li> <li><pre><pre>Of course the best solution is to put that default original John Doe master to fixed, do not let him disappear one will be hidden (that is, explicit definition, so that Zhang three do not each person asked, as long as the problem can be someone automatically answer, of course, this person is the original John Doe master, explicit definition will be silently Contribute)</pre></pre><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">gouzao;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Children<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">children () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Implicit Super constructor Father () is Undefined. Must explicitly invoke another constructor</span></span>System.out.println ("chilren constructor"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main (string[] Args) {children children</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Children (); }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Father {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* or the original John Doe master to hold the good</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Father () {System.out.println (</span></span>"Father constructor"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* of course, the parent class can write other arguments with the constructor, then the subclass calls the default parameterless constructor * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">x</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Father (String x) {System.out.println (x); }}</span></span></pre><p><p></p></p></li> </ol></li> <li><li>Specific application Scenarios <ol> <li>When inheriting the dialog () class <ol> <li>http://blog.itpub.net/25701553/viewspace-697932/</li> </ol></li> <li>Customize a control in Android to inherit the LinearLayout class <ol> <li>Http://zhidao.baidu.com/link?url=oioeyWDrki5D7CcUoxiwV3ZNc3hZX1Vkv8Rfh32XzNr25bPLlTQnGTMDr4kwQ2cWdd3iG91pS3RxsLU5csEgNa<pre class="line mt-5 q-content"></pre></li> </ol></li> <li>The problem is generally that there is no parameterless constructor defined in the parent class to inherit, but no argument constructor is used in the subclass to call the parent class with super</li> </ol></li></li> <li><li>Extension: why some classes do not have parameterless constructors <ol> <li>The dialog class does not have empty construction method reason, because dialog must rely on a component to exist, our construction method is bound to specify who the father of this dialog, empty construction method How to specify this Father component?</li> </ol></li></li> </ol></li> <li><li>  </li></li> </ol><p><p>Think in Java constructor _ Inherit _super () _android_dialog () _ Why some classes do not have constructors themselves to understand</p></p></span>

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.