Toggle theme
문제를 잘 정의하는 것은 문제를 절반 해결한 것이다. - 2023.12
사용자 도구
Toggle theme
로그인
사이트 도구
검색
도구
문서 보기
이전 판
PDF로 내보내기
Fold/unfold all
역링크
최근 바뀜
미디어 관리자
사이트맵
로그인
>
최근 바뀜
미디어 관리자
사이트맵
현재 위치:
start
»
wiki
»
ai
»
python
»
클래스_상세_이해_상속_다중상속
wiki:ai:python:클래스_상세_이해_상속_다중상속
이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요.
====== 클래스 상세 이해(상속, 다중상속) ====== <WRAP left notice 80%> * description : 클래스 상세 이해(상속, 다중상속) * author : 도봉산핵주먹 * email : hylee@repia.com * lastupdate : 2020-06-25 </WRAP> <WRAP clear/> ===== 클래스 상세 이해(상속, 다중상속) ===== ==== 예제 코드 ==== <code python> # Section07-2 # 파이썬 클래스 상세 이해 # 상속, 다중상속 # 예제1 # 상속 기본 # 슈퍼클래스 및 서브클래스 -> 모든 속성, 메소드 사용 가능 class Car: """Parent Class""" def __init__(self, tp, color): self.type = tp self.color = color def show(self): # print('Car Class "Show" Method!') return 'Car Class "Show" Method!' class BmwCar(Car): """Sub Class""" def __init__(self, car_name, tp, color): super().__init__(tp, color) # super() 부모 클레스 self.car_name = car_name def show_model(self) -> None: # -> None 힌트 return 'Your Car Name : %s' % self.car_name class BenzCar(Car): """Sub Class""" def __init__(self, car_name, tp, color): super().__init__(tp, color) self.car_name = car_name def show(self): super().show() return 'Car Info : %s %s %s' % (self.car_name, self.color,self.type) def show_model(self) -> None: return 'Your Car Name : %s' % self.car_name # 일반 사용 print("#==== 일반사용 ====") model1 = BmwCar('520d', 'sedan', 'red') print(model1.color) # Super print(model1.type) # Super print(model1.car_name) # Sub print(model1.show()) # Super print(model1.show_model()) # Sub print() # Method Overriding print("#==== Method Overriding ====") model2 = BenzCar("220d", 'suv', 'black') print(model2.show()) # 부모와 자식 클레스에 똑같은 메소드가 있으면 자식 메소드가 호출됨 print() # Parent Method Call print("#==== Parent Method Call ====") model3 = BenzCar("350s", 'sedan', 'silver') print(model3.show()) print() # Inheritance Info print("#==== Inheritance Info ====") print('Inheritance Info : ', BmwCar.mro()) print('Inheritance Info : ', BenzCar.mro()) print() # 예제2 # 다중 상속 print("#==== 다중 상속 ====") class X(): pass class Y(): pass class Z(): pass class A(X, Y): pass class B(Y, Z): pass class M(B, A, Z): pass # .mro() -> 상속형태를 List형으로 반환한다. print(M.mro()) print(A.mro()) </code> ==== 실행 콘솔 ==== <code console> #==== 일반사용 ==== red sedan 520d Car Class "Show" Method! Your Car Name : 520d #==== Method Overriding ==== Car Info : 220d black suv #==== Parent Method Call ==== Car Info : 350s silver sedan #==== Inheritance Info ==== Inheritance Info : [<class '__main__.BmwCar'>, <class '__main__.Car'>, <class 'object'>] Inheritance Info : [<class '__main__.BenzCar'>, <class '__main__.Car'>, <class 'object'>] #==== 다중 상속 ==== [<class '__main__.M'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.Z'>, <class 'object'>] [<class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class 'object'>] </code> ===== Tip ===== {{tag>도봉산핵주먹 python 상속 다중상속}}
/volume1/web/dokuwiki/data/pages/wiki/ai/python/클래스_상세_이해_상속_다중상속.txt
· 마지막으로 수정됨: 2023/01/13 18:44 (바깥 편집)
문서 도구
문서 보기
이전 판
역링크
PDF로 내보내기
Fold/unfold all
맨 위로