- class Demo_Extends {
- public static void main(String[] args) {
- fu z = new zi("张三",12);
- }
- }
- class fu {
- private String name;
- private int age;
-
- //父类无参构造函数
- fu() {
- System.out.println("父类的无参构造函数");
- }
- //父类有参构造函数
- fu(String name, int age) {
- this.name = name;
- this.age = age;
- System.out.println("父类的有参构造函数");
- System.out.println("父类的姓名"+name+"年龄"+age);
- }
- void say() {
- System.out.println("这是父类");
- }
- }
- class zi extends fu {
-
- //子类无参构造函数
- zi() {
- super();//即使不写系统默认
- System.out.println("子类的无参构造函数");
- }
- //子类有参构造函数
- zi(String name, int age) {
- super(name,age);//通过super(name,age)调用父类有参的构造函数
- System.out.println("子类的有参构造函数");
- System.out.println("子类的姓名"+name+"年龄"+age);
- }
- //重写父类的方法
- void say() {
- System.out.println("这是子类");
- }
- }
复制代码 ---------------------------------------------------------------------------
希望对你有帮助 |