官术网_书友最值得收藏!

Inheritance

The ability to be able to create your own classes that reuse, extend, and modify the behavior that the base class defines is called inheritance. Another important aspect to understand is that a derived class can only directly inherit a single base class.

Does this then mean that you can only inherit the behavior defined in a single base class? Well, yes and no. Inheritance is transitive in nature.

To explain this, imagine that you have three classes:

  • Person
  • Pedestrian
  • Driver

The Person class is the base class. Pedestrian inherits from the Person class and therefore Pedestrian inherits the members declared in the Person class. The Driver class inherits from the Pedestrian class and therefore Driver inherits the members declared in Pedestrian and Person:

This is what is meant when we say that inheritance is transitive. You can only inherit from a single class, but you get all the members that the class you inherit from, inherits itself. You can only inherit from a single class, but you get all the members that the class you are inheriting from, inherits from its base class. Put another way, the Driver class can only inherit from a single base class (in the preceding image, the Pedestrian class). This means that because the Pedestrian class inherits from the Person class, and the Driver class inherits from the Pedestrian class, that the Driver class also inherits the members in the Person class.

主站蜘蛛池模板: 滨州市| 永清县| 合作市| 民权县| 南投市| 原平市| 屯门区| 孟连| 新营市| 巫溪县| 永定县| 平谷区| 连南| 乡城县| 和静县| 茂名市| 栾川县| 阜新市| 盘山县| 黑水县| 万年县| 常州市| 玛曲县| 寻乌县| 札达县| 东宁县| 翁源县| 吉木萨尔县| 班戈县| 伊宁县| 台安县| 美姑县| 栾城县| 西平县| 德令哈市| 鄱阳县| 洪雅县| 娱乐| 余姚市| 明水县| 新安县|