关闭

Inheritance

标签: inheritance继承
116人阅读 评论(0) 收藏 举报
分类:

Inheritance

Q:为什么要继承

You have two ways to differentiate your new derived class from the original base class. The first is quite straightforward: You simply add brand new methods to the derived class. These new methods are not part of the base-class interface. This means that the base class simply didn’t do as much as you wanted it to, so you added more methods. This simple and primitive
use for inheritance is, at times, the perfect solution to your problem. However, you should look closely for the possibility that your base class might also need these additional methods. This process of discovery and iteration of your design happens regularly in object-oriented programming.
Although inheritance may sometimes imply (especially in Java, where the keyword for
inheritance is extends ) that you are going to add new methods to the interface, that’s not
necessarily true. The second and more important way to differentiate your new class is to
change the behavior of an existing base-cla ss method. This is referred to as overriding that
method.

意思是:我们如何区分衍生的class和原来的class,简单来说两方面,1.我们继承核心代码,然后增加我们需要的新变量,新方法 2.我们重写原来就有的方法,从而实现不同的功能。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:95921次
    • 积分:2664
    • 等级:
    • 排名:第15070名
    • 原创:148篇
    • 转载:30篇
    • 译文:24篇
    • 评论:8条
    文章分类
    最新评论