当子类继承父类的时候,若父类没有定义带参的构造方法,则子类可以继承父类的默认构造方法
当父类中定义了带参的构造方法,子类必须显式的调用父类的构造方法
若此时,子类还想调用父类的默认构造方法,必须在父类中明确声明默认的构造方法
package com.gaohui; public class Test {
public static void main(String [] args){
Man man = new Man(24,"Tom");
man.eat();
man.eat("Tom");
man.Exercise();
Woman woman = new Woman(); } } class People{
private int age;
private String name;
private String sex; public People(){
//如果父类没有声明默认的构造方法,子类继承时必须显式调用父类定义的构造方法
} public People(int age, String name){
System.out.println("构造方法一执行了!");
} public void eat(){
System.out.println("People need to eat!");
} public void eat(String name){
System.out.println(name+" needs to eat!");
} } class Man extends People{
public Man (int age, String name){//由于父类的构造方法是有参的,必须显式调用父类的方法
super(age,name);
} public void Exercise(){
System.out.println("I love doing exercise!");
}
} class Woman extends People{ }