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

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.
主站蜘蛛池模板: 遂昌县| 新丰县| 游戏| 拜城县| 安西县| 北海市| 光山县| 台东县| 灵川县| 徐汇区| 高尔夫| 沙坪坝区| 余江县| 全州县| 广安市| 察哈| 彭阳县| 五台县| 石首市| 香格里拉县| 宜宾市| 临夏县| 上杭县| 龙州县| 陕西省| 皋兰县| 乌审旗| 涞源县| 上饶市| 灌南县| 平阴县| 屯留县| 广丰县| 明水县| 璧山县| 乐东| 宁德市| 奉化市| 光泽县| 登封市| 日土县|