JAVA类与对象的初始化

请看一个例子:

<span style="font-family:Times New Roman;">public class Test2
{
	public static void main(String[] args) 
	{
	     SuperInitField p = new SuperInitField();  
	     SuperInitField c = new SubInitField();  
	}
}
 
class SuperInitField 
{  
    public SuperInitField() 
    {  
        System.out.println("parent");  
    }  
    static 
    {  
        System.out.println("static parent");  
    }  
 
}  
 
class SubInitField extends SuperInitField 
{  
    public SubInitField() 
    {  
        System.out.println("child");  
    }  
    static 
    {  
        System.out.println("static child");  
    }  
} </span>
该例子的结果为
static parent
parent
static child
parent
child

搁开原因(请参考链接http://developer.51cto.com/art/201205/337932.htm),解释如下:

首先实例化一个父类对象。这里会先执行父类的static初始块,接着调用父类的构造函数。

<span style="font-family:Times New Roman;"> SuperInitField p = new SuperInitField(); </span>

然后实例化一个子类对象,并赋给父类引用。因为父类的static块只会执行一次,所以先执行子类的static初始块,然后调用父类的构造函数,再调用子类的构造函数。

<span style="font-family:Times New Roman;">SuperInitField c = new SubInitField();  </span>

初始化的顺序为:父类static块和static属性>子类static块和static属性>父类初始化块和非static属性>父类构造函数>子类初始化块和非static属性>子类构造函数。

习题一,请写出运行的结果。

<span style="font-family:Times New Roman;">class A
{
	private int i = 9;
	protected static int j = 1;
	
	static
	{
		System.out.println("--Load First SuperClass of static block Start--");
		System.out.println("j = " + j);
		System.out.println("--Load First SuperClass of static block End--");
	}
	
	public A()
	{
		System.out.println("--Load SuperClass of constructor start--");
		System.out.println("First print j = " + j);
		j = 10;
		m();
		System.out.println("k = " + k);
		System.out.println("Second print j = " + j);
		System.out.println("--Load SuperClass of constructor end--");
	}
	
	private static int k = getInt();
	
	public static int getInt()
	{
		System.out.println("--Load SuperClass.getInt()--");
		return 11;
	}
	
	static
	{
		System.out.println("--Load Second SuperClass of static block Start--");
		System.out.println("j = " + j);
		System.out.println("k = " + k);
		System.out.println("--Load Second SuperClass of static block End--");
	}
	
	public void m()
	{
		System.out.println("SuperClass.m(): " + "j = " + j);
	}
}

class B extends A
{
	private int a = 10;
	
	static
	{
		System.out.println("--Load SubClass of static block start--");
		System.out.println("--Load SubClass of static block end--");
	}
	
	public B()
	{
		System.out.println("--Load SubClass of constructor start--");
		m();
		System.out.println("--Load SubClass of constructor end--");
	}
	
	public void m()
	{
		System.out.println("SubClass.m(), " + "a = " + a);
	}
}

public class Test1 
{
	public static void main(String[] args) 
	{
		System.out.println("--Main Start--");
		A  a = new A();
	}
}</span>
习题二,请写出结果。

<span style="font-family:Times New Roman;">public class Test 
{
	private static Test t1 = new Test();
	private static int i1;
	private static int i2 = getInt();
	
	public Test()
	{
		i1++;
		i2++;
		System.out.println("***************");
	}
	
	public static int getInt()
	{
		System.out.println("i1 = " + i1);
		System.out.println("i2 = " + i2);
		return 11;
	}
	
	public static void main(String[] args) 
	{
		Test t2 = new Test();
		System.out.println("t2.i1 = " + t2.i1);
		System.out.println("t2.i2 = " + t2.i2);
	}

}</span>
习题三,请写出结果。

<span style="font-family:Times New Roman;">class Parent 
{
	 // 静态变量
	 public static String p_StaticField = "父类--静态变量";
	 // 变量
	 public String p_Field = "父类--变量";
	 protected int i = 9;
	 protected int j = 0;
	 // 静态初始化块
	 static 
	 {
		 System.out.println(p_StaticField);
		 System.out.println("父类--静态初始化块");
	 }
	 // 初始化块
	 {
		 System.out.println(p_Field);
		 System.out.println("父类--初始化块");
	 }
	// 构造器
	 public Parent() 
	 {
		 System.out.println("父类--构造器");
		 System.out.println("i=" + i + ", j=" + j);
		 j = 20;
	 }
}

public class SubClass extends Parent
{
	// 静态变量
	public static String s_StaticField = "子类--静态变量";
	// 变量
	public String s_Field = "子类--变量";
	// 静态初始化块
	static
	{
		System.out.println(s_StaticField);
		System.out.println("子类--静态初始化块");
	}
	// 初始化块
	{
		System.out.println(s_Field);
		System.out.println("子类--初始化块");
	}
	// 构造器
	public SubClass() 
	{
		System.out.println("子类--构造器");
		System.out.println("i=" + i + ",j=" + j);
	}
	// 程序入口
	public static void main(String[] args) 
	{
		  System.out.println("子类main方法");
		  new SubClass();
		  new SubClass();
	}
}</span>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值