Search

상속

상속

β€’
상속
β€’
μ˜€λ²„λΌμ΄λ”©
β€’
μ˜ˆμ‹œ μ½”λ“œ

상속(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
볡사