# 상속 (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("이메일 클라이언트")

'Python > 코딩 1시간만에 배우기_파이썬' 카테고리의 다른 글
12. 핸드폰으로 문자보내기 (0) | 2021.12.29 |
---|---|
11. 패키지 & 모듈 (0) | 2021.12.29 |
9. 클래스 (0) | 2021.12.29 |
8. 복습 - 과일 숫자 세는 프로그램 만들기 (0) | 2021.12.29 |
5. 자료구조 - list(리스트) (0) | 2021.12.29 |