posts - 13, comments - 0, trackbacks - 0, articles - 0

由线程名想到的

Posted on 2008-02-23 22:16 Edward's 阅读(43) 评论(0)  编辑  收藏

在写线程的时候,打印线程名发现竟然是 Thread-0 Thread-2
中间没有Thread-1,所以Thread中的init方法执行了两次。想了半天才知道怎么回事。还是看下面的程序吧

package org.edward;

public class StaticTest {
    
public static void main(String[] args) {
        Parent p 
= new Parent(new Child());
        System.out.println(p.getI());
    }

}


class Parent {
    
private static int i;

    
private void init() {
        i
++;
    }


    
public Parent() {
        init();
    }


    
public Parent(Parent p) {
        
this();
    }


    
public static int getI() {
        
return i;
    }


}


class Child extends Parent {

}

原因在于创建子类的时候父类的构造方法就会执行,所以init方法执行了两次。输出2


古之欲明明德於天下者,先治其國;欲治其國者,先齊其家;欲齊其家者,先修其身,欲修其身者,先正其心;欲正其心者,先誠其意;欲誠其意者,先致其知;致知在格物。物格而后知至,知至而后意誠,意誠而后心正,心正而而后身修,身修而后家齊,家齊而后國治,國治而后天下平。


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
成果网帮您增加网站收入
 
相关链接:
网站导航: