第五章---面向对象---1.组合/2.抽象类/3.多态与多态性/4.鸭子类型

1.组合

选课系统 老师类 学生类
组合: 什么有什么 # 继承:什么是什么 代码重用
老师类的数据属性 = 课程类/[] 什么有什么 eg:老师有课程 学生有生日
student1.course1.tell_info()
student1.birth.tell_info()
  1 class People:
  2     school = 'luffycity'
  3     def __init__(self,name,age,sex):
  4         self.name = name
  5         self.age = age
  6         self.sex = sex
  7 
  8 class Teacher(People):
  9     # school = 'luffycity'
 10     def __init__(self,name,age,sex,level,salary):
 11         # self.name = name
 12         # self.age = age
 13         # self.sex = sex
 14         super().__init__(name,age,sex)
 15 
 16         self.level =level
 17         self.salary = salary
 18 
 19     def teach(self):
 20         print('%s is teaching' % self.name)
 21 
 22 
 23 class Student(People):
 24     # school = 'luffycity'
 25 
 26     def __init__(self, name, age, sex, class_time):
 27         # self.name = name
 28         # self.age = age
 29         # self.sex = sex
 30         super().__init__(name,age,sex)
 31 
 32         self.class_time = class_time
 33 
 34     def learn(self):
 35         print('%s is learning' % self.name)
 36 
 37 class Course:
 38     def __init__(self,course_name,course_price,course_period):
 39         self.course_name = course_name
 40         self.course_price = course_price
 41         self.course_period = course_period
 42 
 43     def tell_info(self):
 44         print('课程名<%s> 课程价格<%s> 课程周期<%s>' % (self.course_name,self.course_price,self.course_period))
 45 
 46 class Date:
 47     def __init__(self,year,mon,day):
 48         self.year = year
 49         self.mon = mon
 50         self.day = day
 51 
 52     def tell_info(self):
 53         print('%s-%s-%s' % (self.year,self.mon,self.day))
 54 
 55 teacher1 = Teacher('alex',18,'male',10,3000)
 56 teacher2 = Teacher('egon',18,'female',30,3000)
 57 python = Course('python',3000,'3mons')
 58 linux = Course('Linux',2000,'4mons')
 59 
 60 # 老师和课程不是什么是什么的关系,也就不能使用继承
 61 # 但老师和课程是老师有课程的关系(相当于老师有名字,有性别)
 62 # 可以把课程当做老师的一个属性,这就是组合
 63 teacher1.course = python
 64 teacher2.course = python
 65 
 66 # print(python)
 67 # print(teacher1.course)
 68 # print(teacher2.course)
 69 # # <__main__.Course object at 0x021A6230>
 70 # # <__main__.Course object at 0x021A6230>
 71 # # <__main__.Course object at 0x021A6230>
 72 
 73 # print(teacher1.course.course_period)
 74 # # 3mons
 75 
 76 # teacher1.course.tell_info()
 77 # # 课程名<python> 课程价格<3000> 课程周期<3mons>
 78 
 79 # student1 = Student('王二',20,'female','08:30')
 80 # student1.course1 = python
 81 # student1.course2 = linux
 82 #
 83 # student1.course1.tell_info()
 84 # student1.course2.tell_info()
 85 # # 课程名<python> 课程价格<3000> 课程周期<3mons>
 86 # # 课程名<Linux> 课程价格<2000> 课程周期<4mons>
 87 
 88 # 多重玩法:变成列表
 89 # student1.courses = []
 90 # student1.courses.append(python)
 91 # student1.courses.append(linux)
 92 # student1.courses[0].tell_info()
 93 # student1.courses[1].tell_info()
 94 # # 课程名<python> 课程价格<3000> 课程周期<3mons>
 95 # # 课程名<Linux> 课程价格<2000> 课程周期<4mons>
 96 
 97 student1 = Student('王二',20,'female','08:30')
 98 d = Date(1990,8,24)
 99 
100 student1.birth = d
101 student1.birth.tell_info()
102 # 1990-8-24

2.抽象类

人 猪 狗 都是动物 走的功能 名称不一样 找一种统一的方案
java 有interface 可以解决统一的方案!
在python中根本就没有一个叫做interface的关键字,如果非要去模仿接口的概念
可以借助第三方模块:http://pypi.python.org/pypi/zope.interface
但python 有自己的抽象类
抽象类: 只能被继承 不能被实例化 本质上还是一个类 功能就是规范子类
基于继承演变而来的! 好处降低使用者的使用复杂度 统一标准
 1 # 需要一个方法,将一些类相同的方法统一抽象起来
 2 # 此时就可以为他们抽象出一个父类出来,为了让子类调用方法适合强制使用父类的名称,需要引进一个第三方模块abc
 3 import abc
 4 class Animal(metaclass=abc.ABCMeta):  # 只能被继承,不能被实例化
 5     @abc.abstractmethod
 6     def run(self):
 7         pass
 8 
 9     @abc.abstractmethod
10     def eat(self):
11         pass
12 
13 class People(Animal):
14     def run(self):
15         print('people is walking...')
16 
17     def eat(self):
18         print('people is eating...')
19 
20 class Pig(Animal):
21     def run(self):
22         print('pig is walking...')
23 
24     def eat(self):
25         print('pig is eating...')
26 
27 class Dog:
28     def run(self):
29         print('dog is walking...')
30 
31     def eat(self):
32         print('dog is eating...')
33 
34 peo1 = People()
35 # pig1 = Pig()
36 # dog1 = Dog()
37 #
38 # peo1.run()
39 # pig1.run()
40 # dog1.run()
41 
42 # a = Animal()  # 会报错

3.多态与多态性

多态:指的就是同一类事物有多种形态,
eg:动物有多种形态: 人 狗 猪
文件有多种形态: 文本文件 可执行文件

多态的好处:
多态性:指的是可以在不考虑对象的类型的情况下而直接使用对象
动态多态性:# peo1.talk()
静态多态性:# str int list 可相加+ str+str int+int

多态性的好处:
1.增加了程序的灵活性 -- 建立在多态的前提下
   以不变应万变,不论对象千变万化,使用者都是同一种形式去调用,如func(animal)
2.增加了程序的扩展性
 通过继承animal类创建了一个新的类,使用者无需更改自己的代码,还是用func(animal)去调用
 1 # 多态:同一类事物的多种形态
 2 import abc
 3 class Animal(metaclass=abc.ABCMeta): #同一类事物:动物
 4     @abc.abstractmethod
 5     def talk(self):
 6         pass
 7 
 8 class People(Animal): #动物的形态之一:人
 9     def talk(self):
10         print('say hello')
11 
12 class Dog(Animal): #动物的形态之二:狗
13     def talk(self):
14         print('say wangwang')
15 
16 class Pig(Animal): #动物的形态之三:猪
17     def talk(self):
18         print('say aoao')
19 
20 class Cat(Animal):  # 扩展的
21     def talk(self):
22         print('say miaomiao')
23 
24 # 多态性:指的是可以在不考虑对象的类型的情况下而直接使用对象
25 peo1=People()
26 dog1=Dog()
27 pig1=Pig()
28 cat1=Cat()
29 # peo1.talk()  # 动态多态性
30 # dog1.talk()
31 # pig1.talk()
32 
33 # 不考虑对象的类型的情况下而直接使用对象
34 # eg.学开车 学的是怎么开车 不是怎么开具体哪款车 tesla 宝马等 多态性带来的好处
35 def fun(animal):
36     animal.talk()
37 
38 fun(peo1)
39 fun(pig1)
40 fun(dog1)
41 fun(cat1)  # 在使用的时 直接使用

4.鸭子类型

python 崇尚鸭子类型
鸭子类型: 类与类之间不用共同继承父类,只要做的像一种事物
不继承父类 有共同的方法 给使用者带来了直观性
 1 # class File:
 2 #     def read(self):
 3 #         pass
 4 #     def write(self):
 5 #         pass
 6 
 7 # class Disk:
 8 #     def read(self):
 9 #         print('disk read')
10 #     def write(self):
11 #         print('disk write')
12 #
13 # class Text:
14 #     def read(self):
15 #         print('text read')
16 #     def write(self):
17 #         print('text write')
18 #
19 # disk = Disk()
20 # text = Text()
21 #
22 # disk.read()
23 # disk.write()
24 #
25 # text.read()
26 # text.write()
27 
28 # 序列类型:列表list,元组tuple,字符串str
29 
30 l = list([1,2,3])
31 t = tuple(('a','b'))
32 s = str('hello')
33 
34 # 列表,元组,字符串都像序列类型,所以都可以使用__len__()方法
35 print(l.__len__())
36 print(t.__len__())
37 print(s.__len__())
38 
39 def  len(obj):
40     return obj.__len__()
41 
42 print(len(l))
43 print(len(t))
44 print(len(s))
原文地址:https://www.cnblogs.com/mumupa0824/p/8954038.html