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

Inheritance

Class inheritance is a feature that we can use to extend the functionality of a class, by reusing the capability of another class. Inheritance strongly promotes code reuse. To take a simple example of inheritance, let's say we have a Car class. The general attributes of the vehicle class would be category (such as SUV, sports, sedan, or hatchback), mileage, capacity, and brand. Let's now say that we have another class called Ferrari, which, in addition to the normal car characteristics, has additional characteristics specific to a sports car, such as Horsepower, Topspeed, Acceleration, and PowerOutput. In this situation, we have use an inheritance relationship between the two classes. This type of relationship is an is-a relationship between the child and the base class. We know that a Ferrari is a car. In this case, the car is the base class, and the Ferrari is the child class that inherits common car attributes from the parent class and has extended characteristics of its own. Let's expand the example we discussed previously, where we created an Employee class. We will now create another class called Programmer and see how can we establish an inheritance relation between the two:

The following bullet points explain the preceding code and its structure:

  • Class Programmer(Employee): In the preceding case, we have created another class called Programmer that inherits from the Employee base class. There is an is a relationship between Programmer and Employee. As well as all the variables and methods of the Employee class, the Programmer class defines a few of its own, such as languages, databases, projects, and additional skills.
  • def __init__(self,name,id_gen,lang,db,projects,**add_skills): The init method of the Programmer class takes a few arguments that are self explanatory. Notice the invocation to the (Employee class) super().__init__() super class constructor, which is at line 32. In other high-level languages such as Java and C++, we know that the base class or the super class constructor is automatically called from the child class constructor and that this is the first statement to be executed implicitly from the child class constructor when this is not specified. This is not the case with Python. The base class constructor would not be called implicitly from the child class constructor and we have to explicitly invoke it using the super keyword, as can be seen in line 32.
  • def printSkillDetails(self): This is the method that helps us explore the power of inheritance. We are using the base class variables in this method (iD, name, and salary), along with some variables specific to the Programmer class. This shows how inheritance can be used for reusing code and deriving an is a relation.
  • Lines 52–62: Finally, we create an instance of the Programmer class and invoke the printSkillDetails method.
主站蜘蛛池模板: 靖远县| 河北区| 股票| 潼南县| 丹巴县| 肇东市| 房山区| 澄城县| 安丘市| 齐河县| 虹口区| 平山县| 理塘县| 乌兰浩特市| 嘉荫县| 芒康县| 五常市| 吉林市| 廊坊市| 涟源市| 宁武县| 绥江县| 靖安县| 石棉县| 青阳县| 顺义区| 阿城市| 安丘市| 延津县| 广汉市| 门源| 磴口县| 岳普湖县| 瑞昌市| 清新县| 保亭| 将乐县| 湛江市| 潼关县| 开鲁县| 曲周县|