Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

게임 제작 마스터 클래스

파이썬 수업노트 no. 15 | 파이썬 클래스 상세 이해, 상속, 다중상속 본문

파이썬

파이썬 수업노트 no. 15 | 파이썬 클래스 상세 이해, 상속, 다중상속

엔류 ENRU 2020. 3. 18. 23:11





# Section 07-2 # 파이썬 클래스 상세 이해 # 상속, 다중 상속 # 예제1 # 상속 기본 # 슈퍼클래스(부모)서브클래스(자식) -> 모든 속성, 메소드 사용 가능 # 라면 -> 속성(종류, 회사,, 면 종류, 이름) : 부모 class Car: """Parent Class""" def __init__(self, tp, color): self.type = tp self.color = color def show(self): return 'Car Class "Show Method!"' class BmwCar(Car): """Sub Class""" def __init__(self, car_name, tp, color): super().__init__(tp, color) self.car_name = car_name def show_model(self) -> 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_model(self) -> None: return "Your Car Name : %s" % self.car_name def show(self): print(super().show()) return 'Car Info : %s %s %s' %(self.car_name, self.type, self.color) # 일반 사용 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(model1.__dict__) # Method Overriding(오버라이딩) - 부모에 있는 메소드를 자식에서 똑같은 명칭으로 구현 시 덮어쓴다 model2 = BenzCar("220d", 'suv', "black") print(model2.show()) # 부모에게서 상속을 받을 건 받고 # 다르게 할 부분은 다르게 하고자 할 때 # 목적에 맞게 메소드를 재 구현 # Parent Method Call model3= BenzCar("350s",'sedan','silver') print(model3.show()) # Inheritance Info(상속 정보) print(BmwCar.mro()) print(BenzCar.mro()) # 예제2 # 다중 상속 class X(object): pass class Y(object): pass class Z(object): pass class A(X,Y): pass class B(Y,Z): pass class M(B,A,Z): pass print(M.mro()) print(A.mro())



Comments