Home  >  Article  >  Backend Development  >  Python object-oriented programming (2)

Python object-oriented programming (2)

PHP中文网
PHP中文网Original
2017-07-09 18:13:531098browse

1.Inheritance and derivation

We have said above that everything in Python is an object. We extracted common characteristics and skills from objects and obtained the concept of classes. There are also common characteristics between classes. We can extract common skills and characteristics from classes with common characteristics and skills, which are called parent classes.

For example, teachers and students have names, ages, birthdays, genders, etc., and they can walk, talk, and eat. . . We can summarize a "human" class from teachers and students, called the parent class. Then teachers and students are subclasses of the "human" class. The subclass inherits the parent class and has the characteristics and methods of the parent class.

Inheritance is a relationship between what ‘is’ and something. Inheritance is a method of generating new classes. Of course, the purpose is to reduce code reuse.

The basic form of inheritance is:

class People:
    pass
class Student(People):#People称为基类或者父类
    pass

Supports multiple inheritance in Python, a subclass can inherit multiple parent classes

We can view all inherited parent classes through the __bases__ method, which will return a tuple.

class People:
    pass
class Animals:
    pass
class Student(People,Animals):
    pass

print(Student.__bases__)#(, )
print(People.__bases__)#(,)

You can see that in the People parent class, an object class is also inherited by default. This is the difference between new-style classes and classic classes:
All classes and their subclasses that inherit the object class are called new-style Classes that do not inherit the object class are called classic classes.

In Python 3, the default is the new-style class, while in Python2.X, the default is the classic class

How can inheritance reduce code? See example

class People:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def walk(self):
        print('%s is walkig'%self.name)

class Teacher(People):
    def __init__(self,name,age,level):
        People.__init__(self,name,age)
        self.level=level

t1=Teacher('zhang',18,10)
print(t1.level) #10
print(t1.name)  #zhang          子类可以用父类定义的属性
t1.walk()   #zhang is walking   子类无需定义就可以用父类的方法
print(issubclass(Teacher,People))   #True查看Teacher类是不是People类的子类

As you can see from the above example, the Teacher class inherits the parent class People class, but Teacher has its own unique attribute level. Subclasses can also define their own unique methods, which can even overlap with the methods of the parent class. name, but the execution will be based on the definition of the subclass.

This is called derivation

2. Combination

Inheritance solves the problem of what 'is' something. Then there is another scenario where everything has something. For example, the teacher has a birthday, the student also has a birthday, and the birthday has attributes such as year, month and day. If each class is written, then It's duplicate code. But students and teachers cannot be allowed to inherit the birthday class. At this time, the combination is used. Combination is to solve the problem of what ‘has’ something. See example

class Date:
    def __init__(self,year,mon,day):
        self.year=year
        self.mon=mon
        self.day=day
    def tell_birth(self):
        print('出生于%s年%s月%s日'%(self.year,self.mon,self.day))

class Teacher:
    def __init__(self,name,age,year,mon,day):
        self.name=name
        self.age=age
        self.birth=Date(year,mon,day)
t=Teacher('egon',19,2010,10,10)
print(t.birth)          #<__main__.Date object at 0x0000017E559380F0>
t.birth.tell_birth()    #出生于2010年10月10日

What? Too many parameters? *Learn it, just be happy with it

 1 class Date:
 2     def __init__(self,year,mon,day):
 3         self.year=year
 4         self.mon=mon
 5         self.day=day
 6     def tell_birth(self):
 7         print('出生于%s年%s月%s日'%(self.year,self.mon,self.day))
 8 
 9 class Teacher:
10     def __init__(self,name,age,*args):
11         self.name=name
12         self.age=age
13         self.birth=Date(*args)
14 t=Teacher('egon',19,2010,10,10)
15 print(t.birth)          #<__main__.Date object at 0x0000017E559380F0>
16 t.birth.tell_birth()    #出生于2010年10月10日
View Code

3.Abstract classes and interfaces

Inheritance has two uses: 1. Code reuse, the subclass inherits the method of the parent class

   2. Declare that a subclass is compatible with a certain parent class and define an interface class Interface. The interface class defines some interface names (that is, function names) and does not implement the functions of the interface. The subclass inherits the interface class and implements Functions in the interface

It should be noted that there is no keyword for interface in Python. We can only imitate the functions of the interface.
For example, in Python, everything is a file, so the program is a file, the hardware is a file, and so are text documents. Files, we know what a file is, that is, it can be read and written. Programs and text documents should all have the functions of reading and writing. Let's simulate it

class Interface:
    def read(self):
        pass
    def write(self):
        pass
    
    
class Txt(Interface):
    def read(self):
        print('文本文档的读取方式')
    def write(self):
        print('文本文档的写入方式')
        
class Sata(Interface):
    def read(self):
        print('硬盘文件的读取方式')
    def write(self):
        print('硬盘文件的写入方式')

class process(Interface):
    def read(self):
        print('进程数据的读取方式')
    def write(self):
        print('进程数据的写入方式')
View Code

这么做的意义就是:我们不需要知道子类有什么具体的方法,既然他们继承了文件类,那他们就是文件,那他们就有读和写这两个功能

父类限制了子类子类必须有read和write这两个方法,而且名字也必须一样(当然现在只是我们主观上的限制,一会我们说完抽象类,就可以从代码级别上限制了),这样就实现了统一,模拟了接口的概念,这就是归一化设计。在归一化设计中,只要是基于一个接口设计的类,那么所有的这些类实例化出来的对象,在用法上是一样的

我们再来说一下抽象类:

Python中的抽象类需要导入一个模块来实现。抽象类只能被继承,不能被实现

抽象类的写法:

import abc
class File(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def read(self):
        pass
    @abc.abstractmethod
    def write(self):
        pass
#父类使用了抽象类,那子类就必须继承父类的方法,而且名字也必须一样
#这样就实现了代码级别的限制

class Txt(File):
    def read(self):
        print('文本文档的读取方式')
    def write(self):
        print('文本文档的写入方式')

 

4.继承的实现原理

1)继承顺序:

python支持多继承,当一个类继承多个父类时,继承顺序是怎样的呢?这个顺序在新式类和经典类中是不一样的。

在新式类中,继承顺序是广度优先,在经典类中是深度优先,举个栗子:

图不重要,看内容
在这个图中,H是子类,H继承E,F,G,E,F,G,又分别继承B,C,D,B,C,D,同时继承A

在新式类中的顺序是:H E B F C G D A 

在经典类中的顺序是:H E B A F C G D

2)继承原理:

当我们定义一个类后,Python就会根据上面的继承规律解析出一个继承顺序的列表(MRO列表),可以通过mro()查看,但是这个方法只有在新式类中才有,经典类没有

mro

 

The above is the detailed content of Python object-oriented programming (2). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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