A deposit and withdrawal programming(입/출금 프로그램)
글 작성자: 성비비
- Object orientation
def person_init(name, money):
obj = {'name': name, 'money': money}
obj['give_money'] = Person[1]
obj['get_money'] = Person[2]
obj['show'] = Person[3]
return obj
def give_money(self, other, money):
self['money'] -= money
other['get_money'](other, money)
def get_money(self, money):
self['money'] += money
def show(self):
print('{} : {}'.format(self['name'], self['money']))
Person = person_init, give_money, get_money, show
if __name__ == "__main__":
g = Person[0]('greg', 5000)
j = Person[0]('john', 2000)
g['show'](g)
j['show'](j)
print('')
g['give_money'](g, j, 2000)
g['show'](g)
j['show'](j)
Object orientation using class
class Person:
def __init__(self, name, money):
self.name = name
self.money = money
def give_money(self, other, money):
self.money -= money
other.get_money(money)
def get_money(self, money):
self.money += money
def show(self):
print('{} : {}'.format(self.name, self.money))
if __name__ == "__main__":
g = Person('greg', 5000)
j = Person('john', 2000)
g.show()
j.show()
g.give_money(j, 2000)
print('')
g.show()
j.show()
Can you see the difference?
'Python' 카테고리의 다른 글
Monitor Windows CLI-based processes (0) | 2017.01.11 |
---|