静态代码块 构造代码块 构造函数

class Student {
static {
System.out.println("Student 静态代码块");
}


{
System.out.println("Student 构造代码块");
}


public Student() {
System.out.println("Student 构造方法");
}
}


class StudentDemo {
static {
System.out.println("静态代码块,带你装逼带你飞");
}


public static void main(String[] args) {
System.out.println("我是main方法");


Student s1 = new Student();
Student s2 = new Student();
}

}


看程序写结果2
        class Fu {
            static {
                System.out.println("静态代码块Fu");
            }
    
            {
                System.out.println("构造代码块Fu");
            }
    
            public Fu() {
                System.out.println("构造方法Fu");
            }
        }
    
        class Zi extends Fu {
            static {
                System.out.println("静态代码块Zi");
            }
    
            {
                System.out.println("构造代码块Zi");
            }
    
            public Zi() {
                System.out.println("构造方法Zi");
            }
        }
    
        Zi z = new Zi(); 请执行结果。

你可能感兴趣的:(java,基础)