面向对象2

def school(name,addr,type):

    # school={#这样就写死了,不好
    #     'name':'三小',
    #     'addr':'沙河',
    #     'type':'私立',
    # }


    def kaoshi(school):
        print('%s学校正在考试'%school['name'])

    def zhaosheng(school):
        print('%s%s正在招生'%(school['addr'],school['type']))
    def init(name,addr,type):
        sch = {  # 这样好点,根据变量名传值,最好放到init函数中这样整洁
            'name': name,
            'addr': addr,
            'type': type,
            'kaoshi': kaoshi,
            'zhaosheng': zhaosheng
        }
        return sch
    return init(name,addr,type)#需要有返回值

s1=school('s','a','私立')
s1['zhaosheng'](s1)#函数运行传的值应该是字典型,此处执行的是招生函数
s1['kaoshi'](s1)

 

posted @ 2018-05-15 10:10  未来的技术  阅读(67)  评论(0编辑  收藏  举报