标签:模仿 assm date 它的 老男孩 小学 pig eth display
面向对象的程序设计一(类和对象)
1 ‘‘‘ 2 对象一 3 name="fang" 4 age=28 5 country="shina" 6 7 技能 8 def talk(self): 9 print("is talking") 10 11 def eat(self): 12 print("is eating") 13 14 def sleep(self): 15 print("sleeping") 16 17 对象二 18 name="jie" 19 age=18 20 country="shina" 21 22 技能 23 def talk(self): 24 print("is talking") 25 26 def eat(self): 27 print("is eating") 28 29 def sleep(self): 30 print("sleeping") 31 32 33 对象一 34 name="yi" 35 age=22 36 country="shina" 37 38 技能 39 def talk(self): 40 print("is talking") 41 42 def eat(self): 43 print("is eating") 44 45 def sleep(self): 46 print("sleeping") 47 ‘‘‘ 48 # #程序中的类 49 # class chinese: 50 # #共同的特征 51 # country="china" 52 # 53 # #__init__(self,定义变量, 定义变量, 定义变量) 54 # def __init__(self,name,age): 55 # #只干初始化的活 56 # if not isinstance(name,str): 57 # raise TypeError 58 # self.name=name 59 # self.age=age 60 # 61 # # 共同的技能有 62 # def talk(self): 63 # print("is talking") 64 # 65 # def eat(self): 66 # print("is eating") 67 # 68 # def sleep(self): 69 # print("sleeping") 70 # 71 # #程序中的对象 72 # a=chinese("fang",28) 73 # b=chinese("jie",18) 74 # c=chinese("yi",22) 75 # #a可以成为irchinese类的一个实例,或对象 76 77 78 79 # class chinese: 80 # country="china" 81 # 82 # def __init__(self,name,color,age): 83 # self.name=name #self.name=‘xiaofei‘ 84 # self.color=color #self.color="pink" 85 # self.age=age #self.age=73 86 # 87 # def talk(self): 88 # print(‘is talking‘) 89 # 90 # def eat(self): 91 # print(‘is eating‘) 92 # 93 # def sleep(self): 94 # print("is sleeping") 95 # 96 # p1=chinese(‘xiaofen‘,‘pink‘,73) #实例化:__init__(p1,‘xiaofen‘,‘pink‘,73) 97 # p2=chinese(‘xiaohei‘,‘blank‘,84) #__init__(p2,xiaohei‘,‘blank‘,84) 98 99 ‘‘‘ 100 alex对象 101 name="alex" 102 sex="male" 103 enucation="小学肄业" 104 school="偶的博爱" 105 coures="python" 106 107 技能 108 def talk(self): 109 print("is talking") 110 111 def walk(self): 112 print("walking") 113 114 115 yuanhao对象 116 name="yuanhao‘ 117 sex="male" 118 aducation="研究生" 119 school="偶的博爱" 120 course="python" 121 122 123 124 技能 125 def talk(self): 126 print("is talking") 127 128 def walk(self): 129 print("walking") 130 131 132 #总结类(老男孩python的老师类): 133 #共同的特征 134 school=‘偶的博爱‘ 135 course="python‘ 136 137 #共同的技能 138 def talk(self): 139 print("is talking") 140 def walk(self): 141 print(‘is walking‘) 142 143 ‘‘‘ 144 # 145 # class teacher: 146 # school="偶的博爱" 147 # coure="python" 148 # def __init__(self,name,sex,education): 149 # self.name=name 150 # self.sex=sex 151 # self.education=education 152 # 153 # def talk(self): 154 # print("is talking") 155 # 156 # def walk(self): 157 # print("is walking") 158 # alex=teacher("alex",‘male‘,‘小学肄业‘) 159 # yuanhao=teacher(‘yuanhao‘,‘male‘,‘研究生‘) 160 161 162 # class student: 163 # school=‘偶的博爱‘ 164 # def __init__(self,name,age): 165 # self.name=name 166 # self.age=age 167 # def talk(self): 168 # print(‘is talking‘) 169 # 170 # def walk(self): 171 # print(‘is walking‘) 172 # 173 # t1=student(‘xiasofen‘,17) 174 # t2=student(‘xiaohei‘,18) 175 176 177 178 179 ‘‘‘ 180 dog1 181 #特征 182 color=black_and_white‘ 183 dog_type=‘二哈‘ 184 185 #技能 186 def talk(self): 187 print(‘汪汪汪‘) 188 189 dog2 190 #特征 191 color=‘zongse‘ 192 dog_type=‘泰迪‘ 193 194 #技能 195 def talk(self): 196 print(‘汪汪汪‘) 197 198 199 #总结 200 #共同的特征 201 #共同的技能 202 203 def talk(self): 204 print(‘汪汪汪‘) 205 206 ‘‘‘ 207 208 # class dog: 209 # def __init__(self,color,dog_type): 210 # self.color=color 211 # self.dog_type=dog_type 212 # 213 # def talk(self): 214 # print("汪汪汪") 215 # 216 # dog1=dog(‘black_and_white‘,‘二哈‘) 217 # dog2=dog(‘zongse‘,‘泰迪‘) 218 # 219 # class pig: 220 # def __init__(self,name): 221 # self.name=name 222 # 223 # def talk(self): 224 # print(‘is talking‘) 225 226 # pig1=pig(‘alex‘) 227 # pig2=pig(‘fang‘) 228 # 229 # 230 # class student: 231 # school=‘偶的博爱‘ #数据类型 232 # 233 # def __init__(self,name,age): #函数属性 234 # self.name=name 235 # self.age=age 236 # 237 # def study(self): 238 # print("is studying") 239 # 240 # def walk(self): 241 # print(‘is walking‘) 242 # #类的用法1:实例化产生对象 243 # t1=student(‘xiaofen‘,17) 244 # t2=student(‘xiahei‘,18) 245 246 247 248 #类的用法二:属性应用 249 #数据属性 250 251 # print(student.__dict__) 252 # 253 # print(student.school) #print(student.__dict__[‘school‘]) 254 # student.school=‘oldboy‘ 255 # print(student.school) #print(student.__dict__[‘school‘]) 256 # print(student.__dict__) 257 # 258 # #往数据属性里面添加类容 259 # print(student.__dict__) 260 # student.x=11211111121 261 # print(student.__dict__) 262 # 263 # #删除数据属性里面的类容 264 # del student.x 265 # print(student.__dict__) 266 267 #函数属性 268 269 #school=‘oldboy‘ 270 # class student: 271 # school="偶的博爱" #数据属性 272 # l=[] 273 # 274 # def __init__(self,name,age): #函数属性 275 # self.name=name 276 # self.age=age 277 # #student.l.append(self) 278 # self.l.append(self) 279 # #t1.name=‘xiaohei‘ 280 # #t1.age=17 281 # 282 # def study(self): 283 # print("is studying") 284 # 285 # def walk(self): 286 # print("is walking") 287 # 288 # t1=student(‘xiaohei‘,17) 289 # t2=student(‘xiaofen‘,18) 290 # 291 # print(t1.l) 292 # print(t2.l) 293 # print(student.l) 294 # print(t1,t2) 295 # 296 # #对象本身只有数据属性,但是可以访问类的 297 # print(t1.name) 298 # print(t1.age) 299 # print(t1.__dict__) 300 # print(t2.__dict__) 301 # print(t1.name,t1.age) 302 # print(t2.name,t2.age) 303 # 304 # print(t1.__dict__[‘age‘]) 305 # 306 # 307 # print(t1.school,id(t1.school)) 308 # print(t2.school,id(t2.school)) 309 # print(student.school,id(student.school)) 310 # 311 # t1.school="hahhahah" #为t1数剧属性添加类容 312 # print(t1.__dict__) 313 # print(t2.__dict__) 314 # 315 # student.school=‘oldboy‘ #为数据属性添加类容 316 # print(t1.school) 317 # print(t2.school) 318 319 320 # class foo: 321 # count=0 322 # def __init__(self,name): 323 # self.name=name 324 # foo.count+=1 325 # 326 # f1=foo(‘fang1‘) #foo.count=1 327 # f2=foo(‘fang2‘) #foo.count=2 328 # f3=foo(‘fang3‘) #foo.count=3 329 # 330 # print(f1.count) 331 # print(f2.count) 332 # print(foo.count) 333 334 335 336 # class student: 337 # school=‘偶的博爱‘ #数据属性 338 # l=[] 339 # def __init__(self,name,age): 340 # self.name=name 341 # self.age=age 342 # #student.l.append(self) 343 # self.l.append(self) 344 # #t1.name=‘xiaohei‘ 345 # #t1.age=17 346 # 347 # def study(self): 348 # print(‘%s is studying‘%self.name) 349 # 350 # t1=student(‘alex1‘,18) #student.__init__(t1,‘alex1‘,18) 351 # t2=student("alex2",18) 352 # print(student.__dict__) 353 # print(t1.__dict__) 354 # print(student.study) #类的函数属性 355 # print(t1.study) #对象的绑定方法 356 # print(t1.study) #对象的绑定方法 357 # 358 # student.study(t1) 359 # student.study(t2) 360 # 361 # t1.study() #student.study(t1) 362 # t1.study() #student.study(t2) 363 # 364 # print(type(t1)) 365 # print(student) 366 # 367 # l1=list([1,2]) 368 # l2=list([‘a‘,‘b‘]) 369 # print(type(l1)) 370 # 371 # l1.append(3) 372 # print(l1) 373 # print(l2) 374 # 375 # l1=[] 376 # list.append(l1,3) 377 # print(l1) 378 # 379 # l2=[] 380 # list.append(l1,4) 381 # print(l1) 382 # print(l2) 383 #
面向对象程序设计作业一
1 # 抽象出学生类,实例化出多个学生对象 2 """ 3 学生对象一 4 name=‘徐德轩’ 5 age=21 6 waimao=‘帅‘ 7 xingbie=‘nan‘ 8 zhiwei=‘学生‘ 9 10 def chi(self): 11 print(‘is chiing‘) 12 def he(self): 13 print(‘is heing‘) 14 15 16 学生对象二 17 name=‘王勇’ 18 age=23 19 waimao=‘黑‘ 20 xingbie=‘nan‘ 21 zhiwei=‘学生‘ 22 23 def chi(self): 24 print(‘is chiing‘) 25 def he(self): 26 print(‘is heing‘) 27 28 29 学生对象三 30 name=‘方杰’ 31 age=22 32 waimao=‘一般‘ 33 xingbie=‘nan‘ 34 zhiwei=‘学生‘ 35 36 def chi(self): 37 print(‘is chiing‘) 38 def he(self): 39 print(‘is heing‘) 40 """ 41 # class student: 42 # xingbie=‘nan‘ 43 # zhiwei=‘学生‘ 44 # def __init__(self,name,age,waimao): 45 # self.name=name 46 # self.age=age 47 # self.waimao=waimao 48 # def chi(self): 49 # print(‘is chiing‘) 50 # def he(self): 51 # print(‘is heing‘) 52 # 53 # a=student(‘徐德轩‘,‘21‘,‘帅‘) 54 # b=student(‘王勇‘,‘23‘,‘黑‘) 55 # c=student(‘方杰‘,‘22‘,‘一般‘) 56 57 # 抽象出老师类,实例化出多个老师对象 58 ‘‘‘ 59 老师对象1 60 name=‘alex‘ 61 age=34 62 xingbie=‘男‘ 63 zhiwei=‘老师‘ 64 65 def chi(self): 66 print(‘is chiing‘) 67 68 def shui(self): 69 print(‘is shuiing‘) 70 71 老师对像2 72 name=‘egon‘ 73 age=28 74 xingbie=‘男‘ 75 zhiwei=‘老师‘ 76 77 def chi(self): 78 print(‘is chiing‘) 79 80 def shui(self): 81 print(‘is shuiing‘) 82 83 84 老师对象3 85 name=‘yuanhao‘ 86 age=25 87 xingbie=‘男‘ 88 zhiwei=‘老师‘ 89 90 def chi(self): 91 print(‘is chiing‘) 92 93 def shui(self): 94 print(‘is shuiing‘) 95 ‘‘‘ 96 97 # class tecaher: 98 # xingbie=‘男‘ 99 # zhiwei="老师" 100 # 101 # def __init__(self,name,age): 102 # self.name=name 103 # self.age=age 104 # 105 # def chi(self): 106 # print(‘is chiing‘) 107 # 108 # def shui(self): 109 # print(‘is shuiing‘) 110 # 111 # a=tecaher(‘alex‘,34) 112 # b=tecaher(‘egon‘,28) 113 # c=tecaher(‘yuanhao‘,25) 114 115 # 抽象出狗类,实例化出多个狗对象 116 117 ‘‘‘ 118 狗对象1 119 gou_name="ak‘ 120 aou_age=2 121 yanse=‘heibaise‘ 122 123 124 def jiao(self): 125 print(‘汪汪汪‘) 126 127 128 狗对象2 129 gou_name="sha‘ 130 aou_age=1 131 yanse=‘heise‘ 132 133 def jiao(self): 134 print(‘汪汪汪‘) 135 136 狗对象3 137 gou_name="huhu‘ 138 aou_age=3 139 yanse=‘baise‘ 140 141 def jiao(self): 142 print(‘汪汪汪‘) 143 ‘‘‘ 144 # class dog: 145 # def __iit__(self,dog_name,dog_age,yanse): 146 # self.dog_name=dog_name 147 # self.dog_age=dog_age 148 # self.yanse=yanse 149 # def jiao(self): 150 # print(‘汪汪汪‘) 151 # 152 # gou1=dog(‘ak‘,2,‘heibaise‘) 153 # gou2=dog(‘sha‘,1,‘heise‘) 154 # gou3=dog(‘huhu‘,3,‘baise‘) 155 156 # 抽象出猪类,实例化出多个猪对象 157 ‘‘‘ 158 猪的对象1 159 160 zhu_name=‘shazi‘ 161 nianling=‘8geyue‘ 162 163 164 def jiao(self): 165 print(‘hengheng‘) 166 167 猪的对象2 168 zhu_name=‘chihuo‘ 169 nianling=‘10geyue‘ 170 171 172 def jiao(self): 173 print(‘hengheng‘) 174 175 猪的对象3 176 zhu_name=‘sha‘ 177 nianling=‘12geyue‘ 178 179 180 def jiao(self): 181 print(‘hengheng‘) 182 183 ‘‘‘ 184 185 class zhu: 186 def __init__(self,zhu_name,nianling,): 187 self.zhu_name=zhu_name 188 self.nianling=nianling 189 190 def jiao(self): 191 print(‘hengheng‘) 192 193 zhu1=zhu(‘shazi‘,‘8geyue‘) 194 zhu2=zhu(‘chihuo‘,‘10geyue‘) 195 zhu3=zhu(‘sha‘,‘12geyue‘) 196 197 # 抽象出动物类,实例化出多个动物 198 ‘‘‘ 199 生物对象1 200 ping=‘zhu‘ 201 tiji=‘pang‘ 202 shengyi=‘hengheng‘ 203 204 def zou(self): 205 print(‘四条腿行走‘) 206 207 生物对象2 208 ping=‘gou‘ 209 tiji=‘zhong‘ 210 shengyin=‘wangwang‘ 211 212 def zou(self): 213 print(‘四条腿行走‘) 214 生物对象3 215 ping=‘mao‘ 216 tiji=‘xiao‘ 217 shengyin=‘miao‘ 218 219 def zou(self): 220 print(‘四条腿行走‘) 221 ‘‘‘ 222 223 class shengwu: 224 def __init__(self,ping,tiji,shengyin): 225 self.ping=ping 226 self.tiji=tiji 227 self.shengyin=shengyin 228 def zou(self): 229 print(‘四条腿行走‘) 230 231 sheng1=shengwu(‘zhu‘,‘pang‘,‘hengheng‘) 232 aheng2=shengwu(‘gou‘,‘zhong‘,‘wangwang‘) 233 sheng3=shengwu(‘mao‘,‘xiao‘,‘niao‘) 234 235 # 抽象出生物类,实例化出多个生物 236 ‘‘‘ 237 生物类对象1 238 pingzhong=‘hua‘ 239 240 241 def chuang(self): 242 print(‘美化‘) 243 244 245 生物类对象2 246 pingzhong=‘树‘ 247 248 249 def chuang(self): 250 print(‘美化‘) 251 252 253 生物类对象1 254 pingzhong=‘草‘ 255 256 257 def chuang(self): 258 print(‘美化‘) 259 260 ‘‘‘ 261 # class zhiwu: 262 # def __init__(self,pingzhong): 263 # self.pingzhong=pingzhong 264 # 265 # def chuang(self): 266 # print(‘美化‘) 267 # 268 # zhi1=zhiwu(‘花‘) 269 # zhi2=zhiwu(‘树‘) 270 # zhi3=zhiwu(‘草‘) 271 # 272 273 274 class garen: 275 camp=‘demacia‘ 276 def __init__(self,name,aggressivity,life_value): 277 self.name=name 278 self.aggressivity=aggressivity 279 self.life_value=life_value 280 281 def attack(self,enemy): 282 enemy.life_value-=self.aggressivity 283 284 285 class riven: 286 camp=‘noxus‘ 287 def __init__(self,name,aggressivity,life_value): 288 self.name=name 289 self.aggressivity=aggressivity 290 self.life_value=life_value 291 292 def attack(self,enemy): 293 enemy.life_value=self.aggressivity 294 295 g=garen(‘欧德爆爱‘,100,200) 296 r=riven("矮力渴死",100,300) 297 298 print(r.life_value) 299 g.attack(r) 300 print(r.life_value)
面向对象程序设计二(继承和派生)
1 # class People: 2 # pass 3 # 4 # class Animal: 5 # 6 # class Student(People,Animal): #People基类或者是父类 7 # pass 8 # 9 # 10 # print(Student.__bases__) 11 # print(People.__bases__) 12 # print(Animal.__bases__) 13 14 # 在python3中,所有类默认继承object(在python3当中所有类都是新式类) 15 # 但凡是继承了object类的子类以及该子类的子类,都称为新式类 16 # 没有继承object类的子类成为经典类(在python2 中,没有继承object类,以及它的子类,都是经典类。) 17 18 19 20 21 22 #继承与抽象(先抽象在继承) 23 # 解决代码重用的问题,减少代码允余 24 # 继承是一种什么‘是’什么的管系 25 # class People: 26 # def __init__(self,name,age): 27 # print(People.__init__()) 28 # self.name=name 29 # self.age=age 30 # def walk(self): 31 # print(‘%s is walking‘% self.name) 32 # 33 # 34 # class Teacher(People): 35 # pass 36 # class Student(People): 37 # pass 38 39 # t=Teacher(‘egon‘,18) 40 # #print(t.name,t.age) 41 # #t.walk() 42 # print(t.__dict__) 43 # t.walk() 44 # 45 # s=Student(‘alex‘,13) 46 # print(s.name,s.age) 47 # s.walk() 48 49 50 51 52 53 54 55 56 57 58 #==============> 59 60 # class People: 61 # def __init__(self,name,age): 62 # print(People.__init__()) 63 # self.name=name 64 # self.age=age 65 # def walk(self): 66 # print(‘%s is walking‘% self.name) 67 # 68 # 69 # class Teacher(People): 70 # school=‘偶的博爱‘ 71 # def teach(self): 72 # print(‘%s is teaching‘%self.name) 73 # class Student(People): 74 # def study(self): 75 # print(‘%s is studying‘%self.name) 76 # 77 # t=Teacher(‘egon‘,18) 78 # print(t.school) 79 # t.teach() 80 81 82 83 # class People: 84 # def __init__(self,name,age,sex): 85 # print(People.__init__()) 86 # self.name=name 87 # self.age=age 88 # self.sex=sex 89 # def walk(self): 90 # print(‘%s is walking‘% self.name) 91 # 92 # class Teacher(People): 93 # school=‘偶的博爱‘ 94 # def __init__(self,name,age,sex,level,salary): 95 # self.name=name 96 # self.age=age 97 # self.sex=sex 98 # 99 # self.level=level 100 # self.salary=salary 101 # def teach(self): 102 # print(‘%s is teaching‘%self.name) 103 # class Student(People): 104 # def __init__(self,name,age,sex,group): 105 # self.name=name 106 # self.age=age 107 # self.sex=sex 108 # 109 # self.group=group 110 # def study(self): 111 # print(‘%s is studying‘%self.name) 112 # 113 # t=Teacher(‘egon‘,18) 114 # print(t.school) 115 # t.teach() 116 117 118 119 # class People: 120 # def __init__(self,name,age,sex): 121 # print(People.__init__()) 122 # self.name=name 123 # self.age=age 124 # self.sex=sex 125 # def walk(self): 126 # print(‘%s is walking‘% self.name) 127 # 128 # class Teacher(People): 129 # school=‘偶的博爱‘ 130 # def __init__(self,name,age,sex,level,salary): 131 # People(self,name,age,sex) 132 # self.level=level 133 # self.salary=salary 134 # def teach(self): 135 # print(‘%s is teaching‘%self.name) 136 # class Student(People): 137 # def __init__(self,name,age,sex,group): 138 # People.__init__(self,name,age,sex) 139 # self.group=group 140 # def study(self): 141 # print(‘%s is studying‘%self.name) 142 # 143 # t=Teacher("egon",18,‘male‘,10,30000) #__init__(t,‘egon‘,18,‘male‘,10,30000) 144 # 145 146 147 # class People: 148 # def __init__(self,name,age,sex): 149 # print(People.__init__()) 150 # self.name=name 151 # self.age=age 152 # self.sex=sex 153 # def walk(self): 154 # print(‘%s is walking‘% self.name) 155 # 156 # class Teacher(People): 157 # school=‘偶的博爱‘ 158 # #__init__(t,‘egon‘,18,‘male‘,10,30000) 159 # def __init__(self,name,age,sex,level,salary): 160 # People(self,name,age,sex) 161 # # People(t,‘egon‘,18,"male") 162 # #t.name=‘egon‘ 163 # #t.age=18 164 # #t.sex=‘male‘ 165 # 166 # self.level=level 167 # self.salary=salary 168 # def teach(self): 169 # print(‘%s is teaching‘%self.name) 170 # class Student(People): 171 # def __init__(self,name,age,sex,group): 172 # People.__init__(self,name,age,sex) 173 # self.group=group 174 # def study(self): 175 # print(‘%s is studying‘%self.name) 176 # 177 # t=Teacher("egon",18,‘male‘,10,30000) #__init__(t,‘egon‘,18,‘male‘,10,30000) 178 179 180 181 182 183 # class People: 184 # def __init__(self,name,age,sex): 185 # print(People.__init__()) 186 # self.name=name 187 # self.age=age 188 # self.sex=sex 189 # def walk(self): 190 # print(‘%s is walking‘% self.name) 191 # def foo(self): 192 # print(‘from father %s‘%self.name) 193 # class Teacher(People): 194 # school=‘偶的博爱‘ 195 # #__init__(t,‘egon‘,18,‘male‘,10,30000) 196 # def __init__(self,name,age,sex,level,salary): 197 # People(self,name,age,sex) 198 # # People(t,‘egon‘,18,"male") 199 # #t.name=‘egon‘ 200 # #t.age=18 201 # #t.sex=‘male‘ 202 # 203 # self.level=level 204 # self.salary=salary 205 # def teach(self): 206 # print(‘%s is teaching‘%self.name) 207 # def foo(self): 208 # People.foo(self) 209 # print(‘from teacher‘) 210 # class Student(People): 211 # def __init__(self,name,age,sex,group): 212 # People.__init__(self,name,age,sex) 213 # self.group=group 214 # def study(self): 215 # print(‘%s is studying‘%self.name) 216 # 217 # t=Teacher("egon",18,‘male‘,10,30000) #__init__(t,‘egon‘,18,‘male‘,10,30000) 218 # 219 # t.foo() 220 221 222 223 224 #继承是用来创建新的类的一种方式,好处是可以减少重复代码 225 #继承是类与类之间的关系,是一种什么是什么的关系 226 227 # class Teacher: 228 # def __init__(self,name,age): 229 # self.name=name 230 # self.age=age 231 # def teach(self): 232 # print(‘%s is teaching‘ %self.name) 233 # 234 # class Course: 235 # def __init__(self,name,price,period): 236 # self.name=name 237 # self.price=price 238 # self.period=period 239 # def tell_info(self): 240 # print("课程名<%s> 价钱:<%s> 周期<%s>"%(self.name,self.price,self.period)) 241 242 243 244 # 组合:什么有什么的关系,也是为了减少重复代码 245 # class Date: 246 # def __init__(self,year,mon,day): 247 # self.year=year 248 # self.mon=mon 249 # self.day=day 250 # def tell_birth(self): 251 # print("出生于<%s>年 <%s>月 <%s>日"%(self.year,self.mon,self.day)) 252 # 253 # class Teacher: 254 # def __init__(self,name,age,year,mon,day): 255 # self.name=name 256 # self.age=age 257 # self.birth=Date(year,mon,day) 258 # def teach(self): 259 # print(‘%s is teaching‘ %self.name) 260 # 261 # t=Teacher(‘egon‘,18,1990,2,33) 262 # print(t.name,t.age) 263 # print(t.birth) 264 # print(t.birth.year) 265 # print(t.birth.mon) 266 # print(t.birth.day) 267 268 269 270 # class Date: 271 # def __init__(self,year,mon,day): 272 # self.year=year 273 # self.mon=mon 274 # self.day=day 275 # def tell_birth(self): 276 # print("出生于<%s>年 <%s>月 <%s>日"%(self.year,self.mon,self.day)) 277 # 278 # class Student: 279 # def __init__(self,name,age,year,mon,day): 280 # self.name=name 281 # self.age=age 282 # self.birth=Date(year,mon,day) 283 # def study(self): 284 # print(‘%s is studying‘ %self.name) 285 286 287 # t=Student(‘fang‘,18,1990,2,25) 288 # print(t.name,t.age) 289 # print(t.birth) 290 # print(t.birth.year) 291 # print(t.birth.mon) 292 # print(t.birth.day) 293 294 295 296 297 # class Student(People,Animal): #People基类或者是父类 298 # def __init__(self,name,age): 299 # self.name=name 300 # self.age=age 301 # def study(self): 302 # print("%s is studying"%self.name) 303 # def walk(self): 304 # print(‘%s is walking‘% self.name) 305 # 306 # class Teacher: 307 # school=‘偶的博爱‘ 308 # def __init__(self,name,age): 309 # self.name=name 310 # self.age=age 311 # def teach(self): 312 # print(‘%s is teaching‘%self.name) 313 # def walk(self): 314 # print(‘%s is walking‘% self.name) 315 # 316 # print(Student.__bases__) 317 # print(People.__bases__) 318 # print(Animal.__bases__) 319 320 321 # class People: 322 # def __init__(self,name,age,year,mon,day): 323 # self.name=name 324 # self.age=age 325 # self.birth=Date(year,mon,day) 326 # def walk(self): 327 # print(‘%s is walking‘ %self.name) 328 # class Date: 329 # def __init__(self,year,mon,day): 330 # self.year=year 331 # self.mon=mon 332 # self.day=day 333 # def tell_birth(self): 334 # print("出生于<%s>年 <%s>月 <%s>日"%(self.year,self.mon,self.day)) 335 # 336 # class Student(People): 337 # def __init__(self,name,age,year,mon,day,group): 338 # People.__init__(self,name,age,year,mon,day) 339 # self.group=group 340 # def study(self): 341 # print(‘%s is studying‘ %self.name) 342 # 343 # class Teacher(People): 344 # def __init__(self,name,age,year,mon,day,level,salary): 345 # People.__init__(self,name,age,year,mon,day) 346 # self.level=level 347 # self.salsry=salary 348 # def teach(self): 349 # print(‘%s is teaching‘ %self.name) 350 # 351 # t=Teacher(‘egon‘,18,1990,2,33) 352 # print(t.name,t.age) 353 # print(t.birth) 354 # print(t.birth.year) 355 # print(t.birth.mon) 356 # print(t.birth.day) 357 # 358 # 359 # 360 # t=Student(‘fang‘,18,1990,2,25) 361 # print(t.name,t.age) 362 # print(t.birth) 363 # print(t.birth.year) 364 # print(t.birth.mon) 365 # print(t.birth.day) 366 367 368 369 370 #接口与归一化设计 371 # class Interface: #定义接口Interface类来模仿接口的概念,python中压根就没有interface关键字 372 # def read(self): #定接口函数read 373 # pass 374 # def write(self): #定接口函数write 375 # pass 376 # class Txt(Interface): #文本,具体实现read和write 377 # def read(self): 378 # print(‘文本数据的读取方法‘) 379 # def write(self): 380 # print(‘文本数据的写入方法‘) 381 # class Sata(Interface): #硬盘,具体实现read和write 382 # def read(self): 383 # print(‘硬盘数据的读取方法‘) 384 # def write(self): 385 # print(‘硬盘数据的写入方法‘) 386 # class Process(Interface): 387 # def read(self): 388 # print(‘进程数据的读取方法‘) 389 # def write(self): 390 # print(‘进程数据的写入方法‘) 391 # 392 # 393 # txt=Txt() 394 # disk=Sata() 395 # process=Process() 396 # 397 # 398 # txt.read() 399 # disk.read() 400 # process.read() 401 402 403 404 405 #父类要限制 406 #1 子类必须要有父类的方法 407 #2 子类实现的方法必须跟父类的方法的名字一样 408 # import abc 409 # 410 # class File(metaclass=abc.ABCMeta): 411 # @abc.abstractclassmethod 412 # def read(self): #定接口函数read 413 # pass 414 # @abc.abstractclassmethod 415 # def write(self): #定接口函数write 416 # pass 417 # class Txt(File): #文本,具体实现read和write 418 # def read(self): 419 # print(‘文本数据的读取方法‘) 420 # def write(self): 421 # print(‘文本数据的写入方法‘) 422 # class Sata(File): #硬盘,具体实现read和write 423 # def read(self): 424 # print(‘硬盘数据的读取方法‘) 425 # def write(self): 426 # print(‘硬盘数据的写入方法‘) 427 # class Process(File): 428 # def read(self): 429 # print(‘进程数据的读取方法‘) 430 # def write(self): 431 # print(‘进程数据的写入方法‘) 432 # 433 # 434 # txt=Txt() 435 # disk=Sata() 436 # process=Process() 437 # 438 # 439 # txt.read() 440 # disk.read() 441 # process.read() 442 #
标签:模仿 assm date 它的 老男孩 小学 pig eth display
原文地址:http://www.cnblogs.com/fangjie0410/p/7112565.html