- Hands-On Penetration Testing with Python
- Furqan Khan
- 466字
- 2021-07-02 14:13:58
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.
- Learn Blockchain Programming with JavaScript
- C語(yǔ)言程序設(shè)計(jì)案例教程(第2版)
- Building a Recommendation Engine with Scala
- CKA/CKAD應(yīng)試教程:從Docker到Kubernetes完全攻略
- Visual C++開(kāi)發(fā)入行真功夫
- SQL基礎(chǔ)教程(第2版)
- HTML5與CSS3基礎(chǔ)教程(第8版)
- C專家編程
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級(jí)賬本看區(qū)塊鏈架構(gòu)設(shè)計(jì)
- 奔跑吧 Linux內(nèi)核
- UX Design for Mobile
- Clojure編程樂(lè)趣
- 一覽眾山小:ASP.NET Web開(kāi)發(fā)修行實(shí)錄
- Learning TypeScript
- C++ Windows Programming