Home > Backend Development > Python Tutorial > what is inheritance in python

what is inheritance in python

藏色散人
Release: 2019-07-05 11:56:16
Original
7027 people have browsed it

what is inheritance in python

One of the main features of object-oriented programming (OOP) languages ​​is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and extend it without having to rewrite the original class.

The new class created through inheritance is called "subclass" or "derived class", and the inherited class is called "base class", "parent class" or "super class". The process of inheritance is The process from general to special. In some OOP languages, a subclass can inherit from multiple base classes. But in general, a subclass can only have one base class. To achieve multiple inheritance, it can be achieved through multi-level inheritance.

There are two main ways to implement the concept of inheritance: implementation inheritance and interface inheritance.

Implementation inheritance refers to the ability to use the properties and methods of a base class without additional coding. Interface inheritance refers to using only the names of properties and methods, but the subclass must provide the ability to implement them (the subclass refactors the parent class method).

When considering using inheritance, one thing to note is that the relationship between two classes should be a "belongs to" relationship. For example, Employee is a person and Manager is also a person, so both classes can inherit the Person class. But the Leg class cannot inherit the Person class because the leg is not a person.

OO development paradigm is roughly as follows: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and synthesis) → using classes and instances to design and implement several stages.

Definition of inheritance:

class Person(object):   # 定义一个父类
 
    def talk(self):    # 父类中的方法
        print("person is talking....")  
 
 
class Chinese(Person):    # 定义一个子类, 继承Person类
 
    def walk(self):      # 在子类中定义其自身的方法
        print('is walking...')
 
c = Chinese()
c.talk()      # 调用继承的Person类的方法
c.walk()     # 调用本身的方法
 
# 输出
 
person is talking....
is walking...
Copy after login

Related recommendations: "Python Tutorial"

The above is the detailed content of what is inheritance in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template