站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题请大牛过来看看这道J面试题的打印结果????????

请大牛过来看看这道J面试题的打印结果????????

摘要: 请大牛过来看看这道J面试题的打印结果????????


public class FatherCls{
static{
System.out.println("father static");
}
public FatherCls(){
System.out.println("father cls");
}
}
public calss ChildCls extends FatherCls {
static{
System.out.println("child static");
}
public ChildCls(){
System.out.println("child cls");
}
}
请问如new ChildCls将会打印什么内容????



static{
System.out.println("father static");
}

不知道这个东西是拿来干什么的


father static
child static
father cls
child cls
顺序 父静态 子静态 父实例变量 父类构造方法 子类实例变量 子类构造方法




father static
child static
father cls
child cls
顺序 父静态 子静态 父实例变量 父类构造方法 子类实例变量 子类构造方法

正解


static{block;}
这个东西在你的类载入的时候会执行


public static void main (String args[]){
try{
Class.forName("Test");//载入类
System.out.println("this");
}catch (Exception e) {

}
new Test();
}
如果你这样用,那么输出结果是:
father static//
child static//这两个载入的时候输出。
this//自己写的输入语句。
father cls
child cls//这两个实例化的时候输出


先父类的静态块,然后在子类的静态块
在后是父类的构造函数,最后是子类的构造函数!

father static
child static
father cls
child cls


受教受教!


father static
child static
father cls
child cls


↑返回目录
前一篇: 请教:EJB与hibernate的一些问题
后一篇: XML 和 JAVA