當子類繼承父類的時候,若父類沒有定義帶參的構造方法,則子類可以繼承父類的默認構造方法
當父類中定義了帶參的構造方法,子類必須顯式的調用父類的構造方法
若此時,子類還想調用父類的默認構造方法,必須在父類中明確聲明默認的構造方法
1 package com.gaohui;
2
3 public class Test {
4 public static void main(String [] args){
5 Man man = new Man(24,"Tom");
6 man.eat();
7 man.eat("Tom");
8 man.Exercise();
9 Woman woman = new Woman();
10
11 }
12
13 }
14
15 class People{
16 private int age;
17 private String name;
18 private String sex;
19
20 public People(){
21 //如果父類沒有聲明默認的構造方法,子類繼承時必須顯式調用父類定義的構造方法
22 }
23
24 public People(int age, String name){
25 System.out.println("構造方法一執行了!");
26 }
27
28 public void eat(){
29 System.out.println("People need to eat!");
30 }
31
32 public void eat(String name){
33 System.out.println(name+" needs to eat!");
34 }
35
36
37 }
38
39 class Man extends People{
40 public Man (int age, String name){//由於父類的構造方法是有參的,必須顯式調用父類的方法
41 super(age,name);
42 }
43
44 public void Exercise(){
45 System.out.println("I love doing exercise!");
46 }
47 }
48
49
50 class Woman extends People{
51
52 }