一、java中的执行顺序
1. 父类静态块
2. 自身静态块
3. 父类块
4. 父类构造器
5. 自身块
6. 自身构造器
2. *DOG父类
3. */
4.public class Dog {
5. public Dog() {
6. System.out.println("Dog");
7. }
8. static{
9. System.out.println("super static block");
10. }
11.
12. {
13. System.out.println("super block");
14. }
15.}
2. * 子类藏獒
3. */
4.public class Mastiff extends Dog {
5. public Mastiff() {
6. System.out.println("Mastiff");
7. }
8.
9. {
10. System.out.println("block");
11.
12. }
13. static {
14. System.out.println("static block");
15. }
16.
17. public static void main(String[] args){
18. Mastiff mastiff=new Mastiff();
19. }
20.}
运行的结果为:
super static block
static block
super block
Dog
block
Mastiff
二、JAVA中赋值顺序
1.父类的静态变量赋值
2.自身的静态变量赋值
3.父类成员变量赋值
4.父类块赋值
5.父类构造器赋值
6.自身成员变量赋值
7.自身块赋值
8.自身构造器赋值
2. *DOG父类
3. */
4.public class Dog {
5. public String type="父类成员变量赋的值";
6. public Dog() {
7. System.out.println("父类构造器--type-->"+type);
8. type="父类构造器赋的值";
9. System.out.println("父类构造器----type--->"+type);
10. }
11.
12. {
13. System.out.println("block---type--->"+type);
14. type="父类块赋的值";
15. }
16.}
2. * 子类藏獒
3. */
4.public class Mastiff extends Dog {
5. public String type="成员变量赋的值";
6. public Mastiff() {
7. System.out.println("构造器---type--->"+type);
8. type="构造器赋的值";
9. }
10.
11. public void say(){
12. System.out.println("say---type---->"+type);
13. }
14.
15. {
16. System.out.println("block---type--->"+type);
17. type="块赋的值";
18.
19. }
20.
21. public static void main(String[] args){
22. Mastiff mastiff=new Mastiff();
23. mastiff.say()</span><span style="font-size: medium;">;</span><span style="font-size: medium;">
24. }
25.}
执行结果如下:
block---type--->父类成员变量赋的值
父类构造器--type-->父类块赋的值
父类构造器----type--->父类构造器赋的值
block---type--->成员变量赋的值
构造器---type--->块赋的值
say---type---->构造器赋的值