面向对象设计:OOD 将一类具体事物的数据和动作整合到一起。
面向对象编程:OOP 用定义类+实例/对象的方式去实现面向对象设计。
eg:
学校类:
特征:name,addr,type
动作:考试,招生,开除学生
def school(name,addr,type):#将相关东西放在同一个作用域内 def init(name,addr,type):#将学校进行初始化 school1={ 'name':name, 'addr':addr, 'type':type, 'test':test, 'job':job, } return school1 def test(school): print('%s we are having a test' % school['name']) def job(school): print('%s %s we are jobbing' % (school['name'],school['type'])) return init(name,addr,type) s1=school('oldboy','shanghai','school') s1['test'](s1) print(s1) s2=school('qinghua','beijing','school') print(s2) s2['job'](s2)
>>>
oldboy we are having a test
{'name': 'oldboy', 'addr': 'shanghai', 'type': 'school', 'test': <function school.<locals>.test at 0x101888e18>, 'job': <function school.<locals>.job at 0x10189d048>}
{'name': 'qinghua', 'addr': 'beijing', 'type': 'school', 'test': <function school.<locals>.test at 0x10189d0d0>, 'job': <function school.<locals>.job at 0x10189d158>}
qinghua school we are jobbing