Java继承练习代码
程序员文章站
2022-03-08 20:21:28
...
class Person {
protected Person(){
System.out.println("Person()");
}
Person(int i){
System.out.println("Person("+i+")");
}
}
class father extends Person{
private father(){
System.out.println("father()");
}
father(int i){
/*这里如果不使用super方法,就会执行默认的构造函数【Person()】,如果Person类中有带参数的构造函数,但是没有实现空构造函数,那么这里必须调用Person(int i)*/
/* == 》 所以最好是有一个空构造函数 《 ==*/
// super(1);
System.out.println("father("+i+")");
}
static father getInstance(){
return new father();
}
}
class Test{
public static void main(String[] args) {
father fa = father.getInstance();
System.out.println("_____________________");
father fa1 = new father(1);
}
}