1 #面向对象
2 #3 #特性
4 #class 类 :对一类拥有相同属性的对象的抽象,蓝图,原型 定义对象的共同方法
5 #object 对象:类的实例化后的实体 一个类可以实例化多个对象 每个对象亦有不同的属性
6 #7 #封装: 内部调用对外部是透明的 里面包含类的数据和方法 (内脏= =)
8 #继承: 一个类派生出子类 父亲的属性,方法自动被子类继承 (财产,基因)
9 #多态: 一个接口多种实现, 同一事物表现出多种形态 (公司各司其职)
10 #11 #编程其实是对具体世界的一种抽象
12 #13 #语法
14 #一般情况:调用函数->执行->返回结果->赋值#面向对象中采取的不是这种方式
15 #面向对象:把实例化后的变量名传入 直接往变量中存东西 变量名即self (self用来接受变量名!)
16 #属性
17 #方法
18 #构造函数
19 #实例变量(__init__)和类变量(class)
20 #类变量的用途:大家共用的属性 实例变量中的属性每个对象中有一个
21 #析构函数
22 #实例释放销毁的时候执行,通常用于做一些收尾工作, 如关闭一些数据库链接 打开的一些临时文件
23 #变量名存在 就在用
24
25 #私有方法 私有属性
26 #记住以下原则: 1 写重复代码是非常不好的低级行为
27 #2 你写的代码需要经常变更->可拓展
28
29 ##############################################################################
30 '''
31 class Dog:32 def __init__(self, name):33 self.name = name34 self.__age = 123 #私有属性35
36 def bulk(self):37 print("%s is a dog" % self.name)38
39 def __sleep(self):40 print("大字型")41
42 def show_status(self):43 print(self.__age)44 self.__sleep()45
46 def __del__(self):47 print("%s彻底销毁" % self.name)48
49
50 d1 = Dog(1)51 d2 = Dog(2)52 d3 = Dog(3)53
54 d1.bulk()55 d2.bulk()56 d3.bulk()57
58 d1.show_status()59
60 class Taidi(Dog):61 def __init__(self, name, fur): #重写父类构造方法62 Dog.__init__(self, name)63 self.fur = fur64
65 def bulk(self): #重构父类方法66 Dog.bulk(self) #为什么要传self?67 print("son bulk")68
69
70
71 t1 = Taidi("taidi1", "red")72 t1.bulk()73 print(t1.fur)74 '''
75
76 '''
77 # class People: #经典类78 class People(object): #新式类 主要体现在继承上79 def __init__(self,name,age):80 self.name = name81 self.age = age82 print("People kind init")83
84 def eat(self):85 print("%s can eat a lot !" % self.name)86
87 def talk(self):88 print("talk")89
90 def sleep(self):91 print("sleep")92
93 class Man(People):94 def __init__(self,name,age,money): #参数是少不了的95 self.money = money96 # People.__init__(self,name,age) #普通的继承 #经典类的写法97 super(Man,self).__init__(name,age) #和之前的有什么区别?好处在哪? -> 多继承时 #新式类的写法98 print("Man kind init money : {}".format(self.money))99 def drink(self):100 print("drink")101 class Woman(People):102 def makeup(self):103 print("makeup")104 person1 = Man("Herry", 22, 15000)105 person1.eat()106 person1.drink()107
108 '''
109
110 #多继承
111 class People(object): #新式类 主要体现在继承上
112 def __init__(self, name, age):
113 self.name =name114 self.age =age115 print("People kind init")116
117 def eat(self):
118 print("%s can eat a lot !" % self.name)119
120 def talk(self):
121 print("talk")122
123 def sleep(self):
124 print("sleep")125
126
127 class Relation(object):
128 def make_friends(self, obj):
129 print("%s is making friends with %s" % (self.name, obj.name))130
131
132 class Man(People, Relation):
133 def __init__(self, name, age, money): #参数是少不了的
134 self.money =money135 #People.__init__(self,name,age) #普通的继承 #经典类的写法
136 super(Man, self).__init__(name, age) #和之前的有什么区别?好处在哪? -> 多继承时 #新式类的写法
137 print("Man kind init money : {}".format(self.money))138
139 def drink(self):
140 print("drink")141
142
143 #class Woman(People, Relation):
144 class Woman(Relation, People): #顺序调换的影响?
145 def makeup(self):
146 print("makeup")147
148
149 person1 = Man("Herry", 22, 15000)150 person2 = Woman("马云", 55)151 person1.eat()152 person1.drink()153 person1.make_friends(person2)154 #为什么person2里面会有name属性? 执行顺序 从左到右 但是 在person2实例化的时候 makefriends方法没执行 所以不抱错 等到执行时已经将模型实例化好了 存在People父类的继承属性
155 #多继承时 若遇见几个父类都有相同的方法 只继承第一个父类的
156 #有两种查询策略 一种是:广度优先搜索 另一种是深度优先搜索 python3中都是广度优先
157 #python2的经典类是按照深度优先来继承的 新式类是按照广度优先来继承的 python3的经典类和新式类都是按照广度优先来继承的