비효율적인 함수 호출
name = 'M'
hp = 40
damage = 5
print("name : {0}".format(name))
print('hp : {0}, damage : {1}\n'.format(hp, damage))
name2 = 'T'
hp2 = 150
damage2 = 35
print("name2 : {0}".format(name2))
print('hp2 : {0}, damage2 : {1}\n'.format(hp2, damage2))
def attack(name, location, damage):
print('{0} : {1} attack [damage : {2}]'.format(name, location, damage))
attack(name, '1', damage)
attack(name2, '2', damage2)
Class
class Unit:
def __init__(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
print('generate : {0}'.format(self.name))
print('hp : {0}, damage : {1}'.format(self.hp, self.damage))
m1 = Unit('M', 40, 5)
m2 = Unit('M', 40, 5)
t1 = Unit('T', 150, 35)
w1 = Unit('W', 80, 5)
print('generate : {0}, damage : {1}'.format(w1.name, w1.damage))
w2 = Unit('W_enemie', 80, 5)
w2.ability = True
if w2.ability == True:
print('{0} : ability on'.format(w2.name))
Method
class Unit:
def __init__(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
print('generate : {0}'.format(self.name))
print('hp : {0}, damage : {1}'.format(self.hp, self.damage))
class AttackUnit:
def __init__(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
def attack(self, location):
print('{0} : to {1} attack [damage : {2}]'.format(self.name, location, self.damage))
def damaged(self, damage):
print('{0} : {1} damaged'.format(self.name, damage))
self.hp -= damage
print('{0} : current hp is {1}'.format(self.name, self.hp))
if self.hp <= 0:
print('{0} : destroyed'.format(self.name))
fire1 = AttackUnit('F', 50, 16)
fire1.attack('5')
fire1.damaged(25)
fire1.damaged(25)
상속
class Unit:
def __init__(self, name, hp):
self.name = name
self.hp = hp
class AttackUnit(Unit):
def __init__(self, name, hp, damage):
Unit.__init__(self, name, hp)
self.damage = damage
def attack(self, location):
print('{0} : to {1} attack [damage : {2}]'.format(self.name, location, self.damage))
def damaged(self, damage):
print('{0} : {1} damaged'.format(self.name, damage))
self.hp -= damage
print('{0} : current hp is {1}'.format(self.name, self.hp))
if self.hp <= 0:
print('{0} : destroyed'.format(self.name))
다중 상속
class Unit:
def __init__(self, name, hp):
self.name = name
self.hp = hp
class AttackUnit(Unit):
def __init__(self, name, hp, damage):
Unit.__init__(self, name, hp)
self.damage = damage
def attack(self, location):
print('{0} : to {1} attack [damage : {2}]'.format(self.name, location, self.damage))
def damaged(self, damage):
print('{0} : {1} damaged'.format(self.name, damage))
self.hp -= damage
print('{0} : current hp is {1}'.format(self.name, self.hp))
if self.hp <= 0:
print('{0} : destroyed'.format(self.name))
class Flyable:
def __init__(self, flying_speed):
self.flying_speed = flying_speed
def fly(self, name, location):
print('{0} : to {1} flying [speed : {2}]'.format(name, location, self.flying_speed))
class FlyableAttackUnit(AttackUnit, Flyable):
def __init__(self, name, hp, damage, flying_speed):
AttackUnit.__init__(self, name, hp, damage)
Flyable.__init__(self, flying_speed)
v1 = FlyableAttackUnit('V', 200, 6, 5)
v1.fly(v1.name, 3)
Method Overriding의 필요성
class Unit:
def __init__(self, name, hp, speed):
self.name = name
self.hp = hp
self.speed = speed
def move(self, location):
print('[unit move]')
print('{0} : to {1} move [speed : {2}]'.format(self.name, location, self.speed))
class AttackUnit(Unit):
def __init__(self, name, hp, speed, damage):
Unit.__init__(self, name, hp, speed)
self.damage = damage
def attack(self, location):
print('{0} : to {1} attack [damage : {2}]'.format(self.name, location, self.damage))
def damaged(self, damage):
print('{0} : {1} damaged'.format(self.name, damage))
self.hp -= damage
print('{0} : current hp is {1}'.format(self.name, self.hp))
if self.hp <= 0:
print('{0} : destroyed'.format(self.name))
class Flyable:
def __init__(self, flying_speed):
self.flying_speed = flying_speed
def fly(self, name, location):
print('{0} : to {1} flying [speed : {2}]'.format(name, location, self.flying_speed))
class FlyableAttackUnit(AttackUnit, Flyable):
def __init__(self, name, hp, damage, flying_speed):
AttackUnit.__init__(self, name, hp, 0, damage)
Flyable.__init__(self, flying_speed)
vv1 = AttackUnit('VV', 80, 10, 20)
battle1 = FlyableAttackUnit('B', 500, 25, 3)
vv1.move('11')
battle1.fly(battle1.name, '7')
Method Overriding
class Unit:
def __init__(self, name, hp, speed):
self.name = name
self.hp = hp
self.speed = speed
def move(self, location):
print('[unit move]')
print('{0} : to {1} move [speed : {2}]'.format(self.name, location, self.speed))
class AttackUnit(Unit):
def __init__(self, name, hp, speed, damage):
Unit.__init__(self, name, hp, speed)
self.damage = damage
def attack(self, location):
print('{0} : to {1} attack [damage : {2}]'.format(self.name, location, self.damage))
def damaged(self, damage):
print('{0} : {1} damaged'.format(self.name, damage))
self.hp -= damage
print('{0} : current hp is {1}'.format(self.name, self.hp))
if self.hp <= 0:
print('{0} : destroyed'.format(self.name))
class Flyable:
def __init__(self, flying_speed):
self.flying_speed = flying_speed
def fly(self, name, location):
print('{0} : to {1} flying [speed : {2}]'.format(name, location, self.flying_speed))
class FlyableAttackUnit(AttackUnit, Flyable):
def __init__(self, name, hp, damage, flying_speed):
AttackUnit.__init__(self, name, hp, 0, damage)
Flyable.__init__(self, flying_speed)
def move(self, location):
print('[flying]')
self.fly(self.name, location)
vv1 = AttackUnit('VV', 80, 10, 20)
battle1 = FlyableAttackUnit('B', 500, 25, 3)
vv1.move('11')
battle1.move('7')
vv1.move('11')
Pass
class Unit:
def __init__(self, name, hp, speed):
self.name = name
self.hp = hp
self.speed = speed
def move(self, location):
print('[unit move]')
print('{0} : to {1} move [speed : {2}]'.format(self.name, location, self.speed))
class BuildingUnit(Unit):
def __init__(self, name, hp, location):
pass
sup1 = BuildingUnit('S', 500, '7')
def start():
print('start')
def over():
pass
start()
over()
Super
class Unit:
def __init__(self, name, hp, speed):
self.name = name
self.hp = hp
self.speed = speed
def move(self, location):
print('[unit move]')
print('{0} : to {1} move [speed : {2}]'.format(self.name, location, self.speed))
class AttackUnit(Unit):
def __init__(self, name, hp, speed, damage):
Unit.__init__(self, name, hp, speed)
self.damage = damage
def attack(self, location):
print('{0} : to {1} attack [damage : {2}]'.format(self.name, location, self.damage))
def damaged(self, damage):
print('{0} : {1} damaged'.format(self.name, damage))
self.hp -= damage
print('{0} : current hp is {1}'.format(self.name, self.hp))
if self.hp <= 0:
print('{0} : destroyed'.format(self.name))
class Flyable:
def __init__(self, flying_speed):
self.flying_speed = flying_speed
def fly(self, name, location):
print('{0} : to {1} flying [speed : {2}]'.format(name, location, self.flying_speed))
class FlyableAttackUnit(AttackUnit, Flyable):
def __init__(self, name, hp, damage, flying_speed):
AttackUnit.__init__(self, name, hp, 0, damage)
Flyable.__init__(self, flying_speed)
def move(self, location):
print('[flying]')
self.fly(self.name, location)
class BuildingUnit(Unit):
def __init__(self, name, hp, location):
super().__init__(name, hp, 0)
self.location = location
sup1 = BuildingUnit('S', 500, '7')
다중 상속에서의 Super
class Unit:
def __init__(self):
print('unit init')
class Flyable:
def __init__(self):
print('flyable init')
class FlyableUnit(Unit, Flyable):
def __init__(self):
super().__init__()
drop1 = FlyableUnit()