Java with parameter constructor initialization

Source: Internet
Author: User

</pre><pre name= "code" class= "Java" >package Com.mejustdoit;public class Component1 {public Component1 (int i) {//TODO auto-generated constructor stubSystem.out.println ("Component1" +i);}} Package Com.mejustdoit;public class Component2 {public Component2 (int i) {//TODO auto-generated constructor STUBSYSTEM.O Ut.println ("COmponent2" +i);} Package Com.mejustdoit;public class Component3 {public Component3 (int i) {//TODO auto-generated constructor STUBSYSTEM.O Ut.println ("Component3" +i);} Package Com.mejustdoit;public class Father {Component1 C1 = new Component1 (1); Component2 C2 = new Component2 (2);p ublic Father (int i) {//TODO auto-generated constructor stubSystem.out.println ("Father       "+i); Component3 C3 = New Component3 (3);}} Package Com.mejustdoit;public class Son extends Father {Component1 C1 = new Component1 (4); Component2 C2 = new Component2 (5); Component3 C3 = New Component3 (6);p ublic Son (int i) {super (i); System.out.println ("Son");}} Package Com.mejustdoit;public class PlayfAtherandson {public static void main (string[] args) {new Son (69); System.out.println ("8ioew");}} The results of the operation are as follows: Component11component22father69component33component14component25component36son8ioew The following procedure: Enter main and new Son. (69); Start constructing son object, enter son's constructor public son (int i) {super (i);//TODO auto-generated constructor stubSystem.out.println ("son") ;} I in Son (int i) is assigned a value of 69 and then goes down to Super (i); (I don't know if I'm going to call the construction of the parent class after entering the constructor (either the default super () or super (parameter)) method, the object of the parent class constructed here belongs to the subclass, After the object is created and the parent class variable is initialized, the output component11component22 is then initialized (executed) to the constructor (the default super () or super (parameter)). The output father69component33 is then the variable that initializes the subclass, and the output component14component25component36 then constructs the subclass (that is, The default super () is either super (parameter) below) output son final output 8ioew initialization introduction can refer to <a target=_blank href= "http://blog.csdn.net/dst111188/ article/details/46754075 "> Click to open the link </a> There is some confusion here, hoping to communicate: whether to enter the constructor when the object is created if the class has an inheritance relationship based on Super ()/super ( parameter) into the parent class constructs the parent class after the object is constructed and initializes the subclass object according to the initialization method (which first initializes the variable in the initialization method (including the constructor method))?




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java with parameter constructor initialization

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.