Java杂集-基础

1、代码执行顺序

有这样两个类:
类A:

public class HelloA {

    public HelloA() {
        System.out.println("HelloA");
    }

    {
        System.out.println("I'm A class");
    }

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

类B继承自类A

public class HelloB extends HelloA {

    public HelloB() {
        System.out.println("HelloB");
    }

    {
        System.out.println("I'm B class");
    }

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

测试:

new HelloB();

输出:

static A
static B
I'm A class
HelloA
I'm B class
HelloB
2、Thread的run()和start()方法
public static void main(String[] args) {
        Thread t = new Thread() {
            @Override
            public void run() {
                pong();
            }
        };
        t.run();
        System.out.print("ping");
    }

    private static void pong() {
        System.out.print("pong");
    }

输出:

pongping

如果把t.run();改为t.start();则输出为:

pingpong  或者 pongping

你可能感兴趣的:(Java杂集-基础)