java含参构造函数初始化

</pre><pre name="code" class="java">package com.mejustdoit;

public class Component1 {
public Component1(int i) {
	// TODO Auto-generated constructor stub
	System.out.println("Component1"+i);
}
}

package com.mejustdoit;

public class Component2 {
public Component2(int i) {
	// TODO Auto-generated constructor stub
	System.out.println("COmponent2"+i);
}
}

package com.mejustdoit;

public class Component3 {
public Component3(int i) {
	// TODO Auto-generated constructor stub
	System.out.println("component3"+i);
}
}

package com.mejustdoit;

public class Father {
Component1 c1 = new Component1(1);
Component2 c2 = new Component2(2);

public Father(int i) {
	// TODO Auto-generated constructor stub
	System.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);
public 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");
}
}
运行结果如下:
Component11
COmponent22
Father69
component33
Component14
COmponent25
component36
Son
8ioew
下面说一下过程:进入main后new  Son(69);开始构造Son对象,进入Son的构造函数
public Son(int i) {
		super(i);
		// TODO Auto-generated constructor stub
		System.out.println("Son");
	}
 Son(int i)中的i被赋值69,然后往下执行到super(i);(我不知道是不是进入构造函数后调用父类的构造(要么是默认的Super()要么是Super(参数))方法,这里构造的父类的对象属于子类,然后创建对象后开始
初始化父类变量,输出
Component11
COmponent22
再初始化(执行)构造方法(是 默认的Super()要么是Super(参数)以下内容),输出
Father69
component33
然后就是初始化子类的变量,输出
Component14
COmponent25
component36

接着子类的构造方法(也就是 默认的Super()要么是Super(参数)以下内容)输出
Son
最后输出
8ioew
初始化的介绍可以参考<a target=_blank href="http://blog.csdn.net/dst111188/article/details/46754075">点击打开链接</a>
这里有些疑惑,希望可以交流:
是不是在创建对象时进入构造函数如果该类有继承关系就根据Super()/Super(参数)进入父类构造父类构造对象完成后按照初始化方法初始化子类对象(先初始化变量在初始化方法(包含构造方法))?




 
 
		
 
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值