public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Student stu1=new Demo2_Student();
System.out.println("漂亮的分割线=============================");
stu1=new Demo2_Student();
}
}
class Student {
private static final String TAG = "Student";
static {
System.out.println("Student 静态代码块");
}
{
System.out.println("Student 构造代码块");
}
public Student() {
System.out.println("Student 构造方法");
}
}
class Demo2_Student extends Student {
static {
System.out.println("Demo2_Student静态代码块");
}
{
System.out.println("Demo2_student 构造代码块");
}
public Demo2_Student() {
System.out.println("demo2_student的构造方法");
}
}
java没怎么复习,面试被这个题给坑了.
结果
09-16 07:19:38.702 28102-28102/? I/System.out: Student 静态代码块
09-16 07:19:38.702 28102-28102/? I/System.out: Demo2_Student静态代码块
09-16 07:19:38.702 28102-28102/? I/System.out: Student 构造代码块
09-16 07:19:38.702 28102-28102/? I/System.out: Student 构造方法
09-16 07:19:38.702 28102-28102/? I/System.out: Demo2_student 构造代码块
09-16 07:19:38.702 28102-28102/? I/System.out: demo2_student的构造方法
09-16 07:19:38.702 28102-28102/? I/System.out: 漂亮的分割线=============================
09-16 07:19:38.702 28102-28102/? I/System.out: Student 构造代码块
09-16 07:19:38.702 28102-28102/? I/System.out: Student 构造方法
09-16 07:19:38.702 28102-28102/? I/System.out: Demo2_student 构造代码块
09-16 07:19:38.702 28102-28102/? I/System.out: demo2_student的构造方法