首页 > 其他 > 详细

面向对象

时间:2020-02-03 23:11:19      阅读:70      评论:0      收藏:0      [点我收藏+]

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正在考试

 

面向对象

原文:https://www.cnblogs.com/forhowcar/p/12257925.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!