(16)继承当中的构造方法 super关键字

it2022-05-05  151

 

 

 

package cn.sg.gouzao;

public class People {          private String name;                    public String getName() {         return name;     }

    public void setName(String name) {         this.name = name;     }

    public People() {         super();         System.out.println("people无参构造方法");     }          public People(String name) {         super();         System.out.println(name+"\tpeople有参构造方法");     }

    public void eat() {         System.out.println("People吃东西");     }

}  

 

package cn.sg.gouzao;

public class Teacher extends People{          public Teacher() {         super("asd");         System.out.println("Teacher无参构造方法");     }          public Teacher(String name) {         super("123");         System.out.println("Teacher有参构造方法");     }          public void eat() {         System.out.println("Teacher吃东西");     }     

}  

 

package cn.sg.gouzao; /**  *   * 在继承当中的构造方法  * @author Administrator  *  */ public class Main {          public static void main(String[] args) {                  Teacher teacher = new Teacher();         Teacher teacher2 = new Teacher("校长");         teacher2.eat();         teacher.eat();              }

}

运行结果:

 

 

 

 

 

自己体会  

 

Author:枫叶


最新回复(0)