day09--继承中构造方法的访问特点
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.inmind.extends_constructor04;
|
||||
/*
|
||||
继承中构造方法的访问特点:
|
||||
1.创建子类对象时,一定会先调用父类的构造方法
|
||||
2.super调用父类的构造方法时,必须在子类构造方法的第一行
|
||||
3.super可以调用父类的有参,无参构造方法,但必须在子类构造方法的第一行调用
|
||||
*/
|
||||
public class Demo07 {
|
||||
public static void main(String[] args) {
|
||||
Zi zi = new Zi();
|
||||
System.out.println("程序结束");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.inmind.extends_constructor04;
|
||||
|
||||
public class Fu {
|
||||
int i;
|
||||
|
||||
/*public Fu(){
|
||||
System.out.println("父类的无参构造执行了");
|
||||
}*/
|
||||
|
||||
public Fu(int i){
|
||||
this.i = i;
|
||||
System.out.println("父类的有参构造执行了");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.inmind.extends_constructor04;
|
||||
|
||||
public class Zi extends Fu{
|
||||
|
||||
public Zi(){
|
||||
//super();//如果没写super(),编译器会主动地在第一行主动添加
|
||||
super(1);
|
||||
System.out.println("子类的无参构造执行了");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user