第九章面向对象程序设计:继承( Inheritance )学****目标: ?能通过继承从现有的类建立新类?理解继承如何提高软件的可重用性?理解 protected 成员访问修饰符?了解继承层次中构造函数和析构函数的用法动物猫科动物发出声音进食/饮水捕食猎物喵喵叫喝牛奶捕食老鼠吼叫吃肉捕食大型猎物可重用性通过继承来实现 Introduction ? Inheritance ? Software reusability ? Create new class from existing class ? Absorb existing class ’ s data and behaviors ? Enhance with new capabilities Introduction ? Inheritance ? Derived class( 派生类) inherits from base class( 基类) ? Derived class ? More specialized group of objects ? Behaviors inherited from base class ? Can customize ? Additional behaviors ? Class hierarchy( 类层次结构) ? Direct base class( 直接基类) ? Inherited explicitly (one level up hierarchy) ? Indirect base class( 间接基类) ? Inherited two or more levels up hierarchy Introduction ? Class hierarchy( 类层次结构) ? Single inheritance( 单一继承) ? Inherits from one base class ? Multiple inheritance( 多重继承) ? Inherits from multiple base classes ? Base classes possibly unrelated( 不相干的) Introduction ? Three types of inheritance ? public ? Every object of derived class also object of base class ? Base-class objects not objects of derived classes ? Example: All cars vehicles, but not all vehicles cars Introduction ? Three types of inheritance ? public ? Can access non- private members of base class ? Derived class can effect change to private base-class members ? Through inherited non- private member functions Introduction ? Three types of inheritance ? private ? Alternative position ? protected ? Rarely used Introduction
第九章 面向对象程序设计:继承 来自淘豆网www.taodocs.com转载请标明出处.