码迷,mamicode.com
首页 > 编程语言 > 详细

Java 类的构造器、静态代码块、静态方法执行顺序

时间:2016-12-08 02:07:10      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:12px   style   str   ext   person   name   java   静态方法   end   

public class Husband extends Persona{
/**第二执行
public Husband() {
super("One");
System.out.println("Two");
}
}

public class Wife extends Persona {
/**第三执行
public Wife(){
super("Four");
}
/**第四执行
public Wife(String name) {
super(name);
System.out.println("Three");
}
}

public class Persona {
private String name;
private String sex;
private Integer age;

public Persona(String name){
System.out.println("我的名字是:"+ name);
}
/**省略Seter/getter


public class Test {
/**
* 静态块只执行一次/最先执行的
*/
static {
System.out.println("Here We Go");
}
/**第五执行
public Test(){
System.out.println("This just a test");
}
public static void main(String[] args) {
Husband husband=new Husband();
Wife wife=new Wife();
wife=new Wife("Five");
Test test=new Test();
test.say();

}
/**最后执行
public static void say(){
System.out.println("Say Something");
}
}



输出结果:  

Here We Go

我的名字是:One

Two

我的名字是:Four

我的名字是:Five

Three

This just a test

Say Something


  

Java 类的构造器、静态代码块、静态方法执行顺序

标签:12px   style   str   ext   person   name   java   静态方法   end   

原文地址:http://www.cnblogs.com/personaJava/p/6143128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!