μμ
β’
μμ
β’
μ€λ²λΌμ΄λ©
β’
μμ μ½λ
μμ(Inheritance)
λΆλͺ¨ ν΄λμ€μ λ³μμ λ©μλλ₯Ό μμ ν΄λμ€μμ μ¬μ¬μ©νλ κ²
class λΆλͺ¨ν΄λμ€:
pass
class μμν΄λμ€(λΆλͺ¨ν΄λμ€):
pass
Python
볡μ¬
μ€λ²λΌμ΄λ©
μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μ¬μ μ νλ κ²
μμ μ½λ
β’
λΆλͺ¨ ν΄λμ€ : Robot.py
β’
μμ ν΄λμ€ : DroneRobot.py, CleanRobot.py
# λΆλͺ¨ ν΄λμ€ - Robot
class Robot:
# μμ±μ
def __init__(self, name, power, battery):
self.name = name
self.power = power
self.battery = battery
# λ©μλ - μ μ, μ΄λ, μΆ©μ
def power(self, power):
self.power = power
print('power : ', power)
def move(self, direction):
print('{} (μΌ/)λ‘ μ΄λν©λλ€.'.format(direction))
def charge(self):
self.battery = 100
print('μΆ©μ μ΄ μλ£λμμ΅λλ€.')
# μμ ν΄λμ€ - DroneRobot, CleanRobot
# μμ μ μ : class ν΄λμ€λͺ
(λΆλͺ¨ν΄λμ€):
# λλ‘
class DroneRobot(Robot):
# λΆλͺ¨ ν΄λμ€μ λ³μμ λ©μλλ₯Ό λͺ¨λ μ¬μ¬μ©νλ€.
# λ¨, νλΌμ΄λΉ λ©€λ²λ μμλμ§ μμ
# μ΅λ λμ΄
max_height = 50
# super() : μμ ν΄λμ€μ μμ±μμμ λΆλͺ¨ν΄λμ€μ μμ±μλ₯Ό νΈμΆνλ λ©μλ
def __init__(self, name, power, battery, height):
# self.name = name
# self.power = power
# self.battery = battery
super().__init__(name, power, battery)
self.height = height
# μ€λ²λΌμ΄λ©
# : λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μμ ν΄λμ€μμ μ¬μ μνλ κ²
def move(self, direction, height):
if height > DroneRobot.max_height:
print('{}m μ΄μμΌλ‘λ λΉνν μ μμ΅λλ€'.format(DroneRobot.max_height))
return
self.height = height
print('κ³ λ : {}'.format(height))
print('{} (μΌ/)λ‘ λ°©ν₯μΌλ‘ λΉνν©λλ€.'.format(direction))
# λ‘λ΄ μ²μκΈ°
class CleanRobot(Robot):
max_bin = 50
# μμ±μ
def __init__(self, name, power, battery, bin):
super().__init__(name, power, battery)
self.bin = bin
# λ©μλ μ€λ²λΌμ΄λ©
def move(self, direction):
print('{} μΌ(/λ‘) μ΄λνμ¬ μ²μν©λλ€.'.format(direction))
self.bin += 1 # λ¨Όμ§ ν‘μ
def mapping(self):
print('μ²μν μμμ κΈ°μ΅ν©λλ€.')
# λ¨Όμ§ν΅ λΉμ°κΈ°
def vacate(self):
self.bin = 0
print('λ¨Όμ§ν΅μ λΉμλλ€.')
# κ°μ²΄ μμ±
print('# Robot #')
robot = Robot('ν΄λ¨Όλ‘λ΄', 'ON', 100)
robot.move('μΌμͺ½') # μμͺ½, μλμͺ½, μΌμͺ½, μ€λ₯Έμͺ½
robot.charge()
print('# DroneRotbot #')
drone = DroneRobot('λλ‘ λ‘λ΄', 'ON', 100, 10)
drone.move('μμͺ½', 50)
print('# CleanRobot #')
cleanRobot = CleanRobot('λ‘λ΄μ²μκΈ°', 'ON', 100, 0)
cleanRobot.mapping()
cleanRobot.move('μμͺ½')
cleanRobot.vacate()
Python
볡μ¬
ν΄λμ€ νμΌ λλμ΄μ μμ±ν΄λ³΄κΈ°
β’
Robot.py
β’
DroneRobot.py
β’
CleanRobot.py
β’
Main.py
Robot.py
# Robot.py
class Robot:
def __init__(self, name, power, battery):
self.name = name
self.power = power
self.battery = battery
def power(self, power):
self.power = power
print('power : ', power)
def move(self, direction):
print('{} (μΌ/)λ‘ μ΄λν©λλ€.'.format(direction))
def charge(self):
self.battery = 100
print('μΆ©μ μ΄ μλ£λμμ΅λλ€.')
Python
볡μ¬
DroneRobot.py
# DroneRobot.py
from Robot import Robot
class DroneRobot(Robot):
max_height = 50
def __init__(self, name, power, battery, height):
super().__init__(name, power, battery)
self.height = height
def move(self, direction, height):
if height > DroneRobot.max_height:
print('{}m μ΄μμΌλ‘λ λΉνν μ μμ΅λλ€'.format(DroneRobot.max_height))
return
self.height = height
print('κ³ λ : {}'.format(height))
print('{} (μΌ/)λ‘ λ°©ν₯μΌλ‘ λΉνν©λλ€.'.format(direction))
Python
볡μ¬
CleanRobot.py
# CleanRobot.py
from Robot import Robot
class CleanRobot(Robot):
max_bin = 50
def __init__(self, name, power, battery, bin):
super().__init__(name, power, battery)
self.bin = bin
def move(self, direction):
print('{} μΌ(/λ‘) μ΄λνμ¬ μ²μν©λλ€.'.format(direction))
self.bin += 1
def mapping(self):
print('μ²μν μμμ κΈ°μ΅ν©λλ€.')
def vacate(self):
self.bin = 0
print('λ¨Όμ§ν΅μ λΉμλλ€.')
Python
볡μ¬
Main.py
# Main.py
from Robot import Robot
from DroneRobot import DroneRobot
from CleanRobot import CleanRobot
print('# Robot #')
robot = Robot('ν΄λ¨Όλ‘λ΄', 'ON', 100)
robot.move('μΌμͺ½')
robot.charge()
print('# DroneRotbot #')
drone = DroneRobot('λλ‘ λ‘λ΄', 'ON', 100, 10)
drone.move('μμͺ½', 50)
print('# CleanRobot #')
cleanRobot = CleanRobot('λ‘λ΄μ²μκΈ°', 'ON', 100, 0)
cleanRobot.mapping()
cleanRobot.move('μμͺ½')
cleanRobot.vacate()
Python
볡μ¬