java子类继承父类时先调用父类中的无参构造方法:

2019年2月15日22:55:12 发表评论 877 views
public class Person7
{
    String name;
    int age;
    public Person7(){
        System.out.println("1.public Person7(){}" );

    }
}
class Studentt extends Person7{
    String school;
    public Studentt()
    {
        System.out.println("2.public Student(){}");
    }
}




/**
 * Created by xabcd on 2019/2/15.
 */
public class TestPerson7Student {
    public static void main(String[] args)
    {
        Studentt s = new Studentt();
    }
}
结果:
1.public Person7(){}
2.public Student(){}





自类在实例化时先调用父类中的无参构造方法再调用有参构造方法。




可以改在一个文件中:
/**
 * Created by xabcd on 2019/2/15.
 */
public class Person7
{
    String name;
    int age;
    public Person7(){
        System.out.println("1.public Person7(){}" );

    }
}
class Studentt extends Person7{
    String school;
    public Studentt()
    {
        System.out.println("2.public Student(){}");
    }
    public static void main(String[] args)
    {
        Studentt s = new Studentt();
    }
}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: