package StaticDemo;
class Person{
private String name;
private int age;
//静态代码块
static {
System.out.println("父类静态代码块执行了");
}
//构造代码块
{
System.out.println("父类构造代码块执行了");
}
//无参构造函数
Person(){
System.out.println("父类无参的构造函数执行了");
}
Person(String name,int age){
this.name = name;
this.age = age;
System.out.println("父类有参构造函数执行了");
}
}
class Student extends Person{
//静态代码块
static {
System.out.println("子类静态代码块执行了");
}
//构造代码块
{
System.out.println("子类构造代码块执行了");
}
//子类空参构造函数
Student(){
System.out.println("子类的空参构造函数执行了");
}
//子类有参构造函数
Student(String name,int age){
super(name,age);
System.out.println("子类的有参构造函数执行了");
}
}
public class StaticDemo {
public static void main(String[] args){
//Student s = new Student();
Student s1 = new Student("wangqaing",23);
}
}
直接看结果就ok了! |