Python/코딩 1시간만에 배우기_파이썬
10. 상속
딥런
2021. 12. 29. 22:57
# 상속 (inheritance))
class Person: # initialize(초기화)를 줄여서 init이라고 함
def __init__(self, name, age): # __init__() 함수는 self를 첫 인자로 받고,
# Person()에서 새로 쓸 변수를 설정할 수 있음
self.name = name
self.age = age
def say_hello(self, to_name):
print("안녕! " + to_name + " 나는 " + self.name) # say_hello() 함수의 self라는 인자가 하는 역할
def introduce(self):
print("내 이름은 " + self.name + " 그리고 나는 " + str(self.age) + " 살이야")
# age 는 숫자 타입을 가진 변수로 문자열 타입으로 캐스팅을 해줘야 함
class Police(Person): # Police라는 클래스가 Person이라는 클래스를 상속한다.
def arrest(self, to_arrest):
print("넌 체포됐다, " + to_arrest)
class Programmer(Person):
def program(self, to_program):
print("다음엔 뭘 만들지? 아 이걸 만들어야겠다: " + to_program)
wonie = Person("워니", 20)
jenny = Police("제니", 21)
michael = Programmer("마이클", 22)
jenny.introduce()
jenny.arrest("워니")
michael.introduce()
michael.program("이메일 클라이언트")