【Java 基础篇】【第九课】继承

继承就是为了提高代码的复用率。

利用继承,我们可以避免代码的重复。让Woman类继承自Human类,Woman类就自动拥有了Human类中所有public成员的功能。
我们用extends关键字表示继承:
看代码吧:
 1 class Human
 2 {
 3     /*因为类中显式的声明了一个带参数构造器,所以默认的构造器就不存在了,但是你在子类的构造器中并没有显式
 4      * 的调用父类的构造器(创建子类对象的时候,一定会去调用父类的构造器,这个不用问为什么),没有显式调用
 5      * 的话,虚拟机就会默认调用父类的默认构造器,但是此时你的父类的默认构造器已经不存在了,这也就是为什么父
 6      * 类中必须保留默认构造器的原因。
 7      * PS.应该养成良好的编程习惯,任何我们自己定义的类,都显式的加上默认的构造器,以后更深入的学习之后,
 8      * 会发现有很多好处
 9      */
10     public Human()
11     {
12     }
13     public Human(int h)
14     {
15         System.out.println("human construction");
16         this.height = h;
17     }
18     
19     public int getHeight()
20     {
21         return this.height;
22     }
23     
24     public void growHeight(int h)
25     {
26         this.height = this.height + h;
27     }
28     
29     public void breath()
30     {
31         System.out.println("hu.....hu.....");
32     }
33     
34     private int height;
35 }
36 
37 
38 class Woman extends Human
39 {
40     public Human giveBirth()
41     {
42         System.out.println("Give birth");
43         return (new Human(20));
44     }
45 }
46 
47 
48 public class test 
49 {
50     public static void main(String[] args)
51     {
52         Woman girl = new Woman();
53         girl.breath();
54         girl.growHeight(100);
55         System.out.println(girl.getHeight());
56         
57         Human baby = girl.giveBirth();
58         System.out.println(baby.getHeight());
59         baby.growHeight(12);
60         System.out.println(baby.getHeight());
61     }
62 
63 }

输出:

hu.....hu.....
100
Give birth
human construction
20
32

需要注意 的地方:

1.子类中要是要调用基类的成员变量的话,成员变量必须是 protected 或者是 public;

2.子类中可以使用super关键字来指代基类对象,使用super() 相当于调用基类的构造函数,super.成员也可以访问。

看代码吧:

 1 class Human
 2 {
 3     
 4     public Human(int h)
 5     {
 6         System.out.println("human construction");
 7         this.height = h;
 8     }
 9     
10     public int getHeight()
11     {
12         return this.height;
13     }
14     
15     public void growHeight(int h)
16     {
17         this.height = this.height + h;
18     }
19     
20     public void breath()
21     {
22         System.out.println("hu.....hu.....");
23     }
24     
25     protected int height;
26 }
27 
28 
29 class Woman extends Human
30 {
31     public Woman(int h)
32     {
33         super(h);
34         System.out.println("Woman construction");
35     }
36     
37     public Human giveBirth()
38     {
39         System.out.println("Give birth");
40         return (new Human(20));
41     }
42 }
43 
44 
45 public class test 
46 {
47     public static void main(String[] args)
48     {
49         Woman girl = new Woman(20);
50         girl.breath();
51         girl.growHeight(100);
52         System.out.println(girl.getHeight());
53         
54         Human baby = girl.giveBirth();
55         System.out.println(baby.getHeight());
56         baby.growHeight(12);
57         System.out.println(baby.getHeight());
58     }
59 
60 }

输出结果:
human construction
Woman construction
hu.....hu.....
120
Give birth
human construction
20
32

原文地址:https://www.cnblogs.com/by-dream/p/3967123.html