-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_inheritance.py
More file actions
142 lines (109 loc) · 3.61 KB
/
class_inheritance.py
File metadata and controls
142 lines (109 loc) · 3.61 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 사람 클래스로 학생 클래스 만들기
class Person:
def greeting(self):
print('안녕하세요.')
class Student(Person):
def study(self):
print('공부하기')
james = Student()
james.greeting() # 안녕하세요.: 기반 클래스 Person의 메서드 호출
james.study() # 공부하기: 파생 클래스 Student에 추가한 study 메서드
# 포함 관계
class Person:
def greeting(self):
print('안녕하세요.')
class PersonList:
def __init__(self):
self.person_list = [] # 리스트 속성에 Person 인스턴스를 넣어서 관리
def append_person(self, person): # 리스트 속성에 Person 인스턴스를 추가하는 함수
self.person_list.append(person)
# 기반 클래스의 속성 사용하기
class Person:
def __init__(self):
print('Person __init__')
self.hello = '안녕하세요.'
class Student(Person):
def __init__(self):
print('Student __init__')
self.school = '파이썬 코딩 도장'
james = Student()
print(james.school)
print(james.hello) # 기반 클래스의 속성을 출력하려고 하면 에러가 발생함
# super()로 기반 클래스 초기화하기
class Person:
def __init__(self):
print('Person __init__')
self.hello = '안녕하세요.'
class Student(Person):
def __init__(self):
print('Student __init__')
super().__init__() # super()로 기반 클래스의 __init__ 메서드 호출
self.school = '파이썬 코딩 도장'
james = Student()
print(james.school)
print(james.hello)
# 메서드 오버라이딩 사용하기
class Person:
def greeting(self):
print('안녕하세요.')
class Student(Person):
def greeting(self):
print('안녕하세요. 저는 파이썬 코딩 도장 학생입니다.')
james = Student()
james.greeting()
# 다중 상속 사용하기
class Person:
def greeting(self):
print('안녕하세요.')
class University:
def manage_credit(self):
print('학점 관리')
class Undergraduate(Person, University):
def study(self):
print('공부하기')
james = Undergraduate()
james.greeting() # 안녕하세요.: 기반 클래스 Person의 메서드 호출
james.manage_credit() # 학점 관리: 기반 클래스 University의 메서드 호출
james.study() # 공부하기: 파생 클래스 Undergraduate에 추가한 study 메서드
# 다이아몬드 상속
class A:
def greeting(self):
print('안녕하세요. A입니다.')
class B(A):
def greeting(self):
print('안녕하세요. B입니다.')
class C(A):
def greeting(self):
print('안녕하세요. C입니다.')
class D(B, C):
pass
x = D()
x.greeting() # 안녕하세요. B입니다.
# 메서드 탐색 순서 확인하기
D.mro()
x = D()
x.greeting() # 안녕하세요. B입니다.
# 추상 클래스 사용하기
from abc import *
class StudentBase(metaclass=ABCMeta):
@abstractmethod
def study(self):
pass
@abstractmethod
def go_to_school(self):
pass
class Student(StudentBase):
def study(self):
print('공부하기')
def go_to_school(self):
print('학교가기')
james = Student()
james.study()
james.go_to_school()
# 추상 메서드를 빈 메서드로 만드는 이유
@abstractmethod
def study(self):
pass # 추상 메서드는 호출할 일이 없으므로 빈 메서드로 만듦
@abstractmethod
def go_to_school(self):
pass # 추상 메서드는 호출할 일이 없으므로 빈 메서드로 만듦