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

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.
主站蜘蛛池模板: 甘肃省| 闽侯县| 荥经县| 大邑县| 策勒县| 军事| 保定市| 长宁区| 榆树市| 桐乡市| 韶关市| 汾西县| 东光县| 桓仁| 宁海县| 马边| 宣威市| 鄂州市| 潜山县| 黑河市| 锡林浩特市| 烟台市| 大名县| 徐州市| 曲沃县| 桑日县| 临沭县| 巴楚县| 铜山县| 石屏县| 阳城县| 定安县| 中卫市| 新宁县| 黔西| 左权县| 淄博市| 宜良县| 通州区| 临洮县| 司法|