面向对象

1.在子类中重用父类的属性
在子类派生出的新的方法中重用父类的方法:有两种实现方式:
方式一:指名道姓(不依赖继承)
Hero.attack(self,enemy) # 指名道姓 不依赖继承
Hero.__init__(self,nickname,life_value,aggressivity) # 指名道姓 不依赖继承
方式二:super() (依赖继承) super() 会从对象的(MRO)列表中找
super(Garen,self).attack(enemy) # 依赖继承
super(Garen,self).__init__(nickname,life_value,aggressivity) # 依赖继承 py2 写法
super().__init__(nickname,life_value,aggressivity) # py3 写法
  1 # class Hero:
  2 #     def __init__(self, nickname, life_value, aggressivity):
  3 #         self.nickname = nickname
  4 #         self.life_value = life_value
  5 #         self.aggressivity = aggressivity
  6 #
  7 #     def attack(self, enemy):
  8 #         enemy.life_value -= self.aggressivity
  9 #
 10 # class Garen(Hero):
 11 #     camp = 'Demacia'
 12 #     def attack(self, enemy):
 13 #         Hero.attack(self,enemy)  # 指名道姓 不依赖继承
 14 #         print('from Garen Class')
 15 #
 16 # class Riven(Hero):
 17 #     camp = 'Noxus'
 18 #
 19 # g1 = Garen('草丛伦',100,30)
 20 # r1 = Riven('锐雯雯',80,50)
 21 #
 22 # print(r1.life_value)
 23 # g1.attack(r1)
 24 # print(r1.life_value)
 25 
 26 # ---------------------------------------------------------------------
 27 
 28 # class Hero:
 29 #     def __init__(self, nickname, life_value, aggressivity):
 30 #         self.nickname = nickname
 31 #         self.life_value = life_value
 32 #         self.aggressivity = aggressivity
 33 #
 34 #     def attack(self, enemy):
 35 #         enemy.life_value -= self.aggressivity
 36 #
 37 # class Garen(Hero):
 38 #     camp = 'Demacia'
 39 #
 40 #     def __init__(self, nickname, life_value, aggressivity,weapon):
 41 #         Hero.__init__(self,nickname,life_value,aggressivity) # 指名道姓 不依赖继承
 42 #         self.weapon = weapon
 43 #
 44 #     def attack(self, enemy):
 45 #         Hero.attack(self,enemy)  # 指名道姓 不依赖继承
 46 #         print('from Garen Class')
 47 #
 48 # class Riven(Hero):
 49 #     camp = 'Noxus'
 50 #
 51 # g1 = Garen('草丛伦',100,30,'金箍棒')
 52 # print(g1.__dict__)
 53 
 54 # ---------------------------------------------------------------------
 55 
 56 # class Hero:
 57 #     def __init__(self, nickname, life_value, aggressivity):
 58 #         self.nickname = nickname
 59 #         self.life_value = life_value
 60 #         self.aggressivity = aggressivity
 61 #
 62 #     def attack(self, enemy):
 63 #         enemy.life_value -= self.aggressivity
 64 #
 65 # class Garen(Hero):
 66 #     camp = 'Demacia'
 67 #     def attack(self, enemy):
 68 #         super(Garen,self).attack(enemy)  # 依赖继承
 69 #         print('from Garen Class')
 70 #
 71 # class Riven(Hero):
 72 #     camp = 'Noxus'
 73 #
 74 # g1 = Garen('草丛伦',100,30)
 75 # r1 = Riven('锐雯雯',80,50)
 76 #
 77 # print(r1.life_value)
 78 # g1.attack(r1)
 79 # print(r1.life_value)
 80 
 81 # ---------------------------------------------------------------------
 82 
 83 # class Hero:
 84 #     def __init__(self, nickname, life_value, aggressivity):
 85 #         self.nickname = nickname
 86 #         self.life_value = life_value
 87 #         self.aggressivity = aggressivity
 88 #
 89 #     def attack(self, enemy):
 90 #         enemy.life_value -= self.aggressivity
 91 #
 92 # class Garen(Hero):
 93 #     camp = 'Demacia'
 94 #
 95 #     def __init__(self, nickname, life_value, aggressivity,weapon):
 96 #         # super(Garen,self).__init__(nickname,life_value,aggressivity)  # 依赖继承  py2这样写
 97 #         super().__init__(nickname,life_value,aggressivity)   # py3这样写
 98 #         self.weapon = weapon
 99 #
100 #     def attack(self, enemy):
101 #         Hero.attack(self,enemy)  # 知名道姓 不依赖继承
102 #         print('from Garen Class')
103 #
104 # class Riven(Hero):
105 #     camp = 'Noxus'
106 #
107 # g1 = Garen('草丛伦',100,30,'金箍棒')
108 # print(g1.__dict__)
109 
110 # ---------------------------------------------------------------------
111 
112 class A:
113     def f1(self):
114         print('from A')
115         super().f1()
116 
117 class B:
118     def f1(self):
119         print('from B')
120 
121 class C(A,B):
122     pass
123 
124 # print(C.mro())
125 # [<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
126 c = C()
127 c.f1()
128 # from A
129 # from B
2.组合
选课系统 老师类 学生类
组合: 什么有什么 # 继承:什么是什么 代码重用
老师类的数据属性 = 课程类/[] 什么有什么 eg:老师有课程 学生有生日
student1.course1.tell_info()
student1.birth.tell_info()
 1 class People:
 2     school = 'luffycity'
 3 
 4     def __init__(self,name,age,sex):
 5         self.name=name
 6         self.age=age
 7         self.sex=sex
 8 
 9 
10 class Teacher(People):
11     def __init__(self,name,age,sex,level,salary):
12         super().__init__(name,age,sex)
13         self.level=level
14         self.salay=salary
15 
16     def teach(self):
17         print('%s is teaching' %self.name)
18 
19 
20 class Student(People):
21     def __init__(self,name,age,sex,class_time):
22         super().__init__(name,age,sex)
23         self.class_time=class_time
24 
25     def learn(self):
26         print('%s is teaching' %self.name)
27 
28 
29 class Course:
30     def __init__(self,course_name,course_price,course_period):
31         self.course_name=course_name
32         self.course_price=course_price
33         self.course_period=course_period
34 
35     def tell_info(self):
36         print('课程名<%s> 课程价钱<%s> 课程周期<%s>'%(self.course_name,self.course_price,self.course_period))
37 
38 
39 class Date:
40     def __init__(self,year,month,day):
41         self.year=year
42         self.month=month
43         self.day=day
44 
45     def tell_info(self):
46         print('%s-%s-%s'%(self.year,self.month,self.day))
47 
48 
49 # teacher1=Teacher('alex',18,'male',10,3000)
50 # teacher2=Teacher('egon',28,'male',30,3000)
51 #
52 # python=Course('python',3000,'3mons')
53 # linux=Course('linux',2000,'4mons')
54 # print(python.course_name)
55 # teacher1.course=python
56 # teacher2.course=python
57 
58 # print(python)
59 # print(teacher1.course)
60 # print(teacher2.course)
61 # print(teacher1.course.course_name) # teacher1的教的课程
62 # print(teacher2.course.course_name)
63 
64 # teacher1.course.tell_info()
65 
66 # student1=Student('egon',20,'female','08:30:00')
67 # student1.course1=python
68 # student1.course2=linux
69 
70 # student1.course1.tell_info()
71 # student1.course2.tell_info()
72 
73 # student1.course=[]
74 # student1.course.append(python)
75 # student1.course.append(linux)
76 # student1.course[0].tell_info()
77 # student1.course[1].tell_info()
78 
79 # 需求 日期类 学生和生日的关系 学生有生日
80 student1=Student('egon',20,'female','08:30:00')
81 d = Date(1988,4,20)
82 python=Course('python',3000,'3mons')
83 student1.birth=d
84 student1.birth.tell_info()
85 student1.course=python
86 student1.course.tell_info()
3.抽象类
人 猪 狗 都是动物 走的功能 名称不一样 找一种统一的方案
java 有interface 可以解决统一的方案!
在python中根本就没有一个叫做interface的关键字,如果非要去模仿接口的概念
可以借助第三方模块:http://pypi.python.org/pypi/zope.interface
但python 有自己的抽象类
抽象类: 只能被继承 不能被实例化 本质上还是一个类 功能就是规范子类
基于继承演变而来的! 好处降低使用者的使用复杂度 统一标准
 1 import abc
 2 class Animal(metaclass=abc.ABCMeta):   # 通过抽象类把子类的标准规范起来了
 3     all_type = 'animal'
 4     @abc.abstractmethod
 5     def run(self):
 6         pass
 7 
 8     @abc.abstractmethod
 9     def eat(self):
10         pass
11 
12 animal = Animal()   # 不能实例化抽象类
13 
14 class People(Animal):
15     def run(self):
16         print('people is runing')
17 
18     def eat(self):
19         print('people is eatint')
20 
21 class Pig(Animal):
22     def run(self):
23         print('pig is runing')
24 
25     def eat(self):
26         print('pig is eating')
27 
28 class Dog(Animal):
29     def run(self):
30         print('dog is runing')
31 
32     def eat(self):
33         print('dog is eating')
34 
35 # peo1=People()
36 # pig1=Pig()
37 # dog1=Dog()
38 #
39 # peo1.run()
40 # peo1.eat()
41 # pig1.run()
42 # dog1.run()
43 #
44 # print(peo1.all_type)
4.多态与多态性
多态:指的就是同一类事物有多种形态,
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)  # 在使用的时 直接使用
5.鸭子类型
python 崇尚鸭子类型
鸭子类型: 类与类之间不用共同继承父类,只要做的像一种事物
不继承父类 有共同的方法 给使用者带来了直观性
 1 # class Disk:
 2 #     def read(self):
 3 #         print('disk read')
 4 #
 5 #     def write(self):
 6 #         print('disk write')
 7 #
 8 # class Text:
 9 #     def read(self):
10 #         print('text read')
11 #
12 #     def write(self):
13 #         print('text write')
14 #
15 # # f=open()
16 # # f.read()
17 # # f.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 # 同一种事物的三种形态 只要是序列类型 就可以__len__
30 l = list([1,2,3])
31 t = tuple(('a','b','c'))
32 s = str('hello')
33 
34 # print(l.__len__())
35 # print(t.__len__())
36 # print(s.__len__())
37 
38 # def len(obj):  # 内置
39 #     return obj.__len__()
40 
41 print(len(l))
42 print(len(t))
43 print(len(s))
原文地址:https://www.cnblogs.com/alice-bj/p/8540650.html