面向对象

1、原始代码

 1 def school(name,addr,type):
 2     def kao_shi(school):   #功能性用函数
 3         print('%s正在考试'%school['name'])
 4 
 5     def zhao_sheng(school):
 6         print('%s招生的地址是:%s'%(school['name'],school['addr']))
 7     school01={
 8         'name':name,
 9         'addr':addr,
10         'type':type,
11         'kao_shi':kao_shi,#建立与函数def kao_shi的联系
12         'zhao_sheng':zhao_sheng   #建立与函数def zhao_sheng的联系
13 
14     }    #描述性的内容用字典
15     return school01
16 
17 s1=school('hncj','xinchengqu','公立')
18 s1['zhao_sheng'](s1)
19 s1['kao_shi'](s1)
20 
21 >>>
22 hncj招生的地址是:xinchengqu
23 hncj正在考试

 2、二次改进

 1 def school(name,addr,type):
 2     def kao_shi(school):   #功能性用函数
 3         print('%s正在考试'%school['name'])
 4 
 5     def zhao_sheng(school):
 6         print('%s招生的地址是:%s'%(school['name'],school['addr']))
 7     def init(name,addr,type):
 8         school01={
 9             'name':name,
10             'addr':addr,
11             'type':type,
12             'kao_shi':kao_shi,
13             'zhao_sheng':zhao_sheng
14 
15         }    #描述性的内容用字典
16         return school01
17     return init(name,addr,type)
18 
19 s1=school('hncj','xinchengqu','公立')
20 s1['zhao_sheng'](s1)
21 s1['kao_shi'](s1)
22 
23 >>>
24 hncj招生的地址是:xinchengqu
25 hncj正在考试

 

posted @ 2020-02-03 23:00  卡子  阅读(121)  评论(0编辑  收藏  举报