-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday22_0325_class_02.py
More file actions
195 lines (145 loc) · 6.92 KB
/
day22_0325_class_02.py
File metadata and controls
195 lines (145 loc) · 6.92 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
""" 클래스 객체/오브젝트 : (객체 지향 프로그래밍 - OOP: Obj. Oriented Progmng)
클래스변수 / 인스턴스변수는, 변수의 접근 권한(소유권)에 관한 문제
- '클래스 객체'를 독립시키려면, 접근권한, 소유권, 상호관계를 명확히 해야 함
@staticmethod = 클래스 객체와 상관없지만 끼워넣은 매서드
@classmethod = 인스턴스로 접근해서 클래스변수를 변경시키는 매서드
"""
import _script_run_utf8
_script_run_utf8.main()
SEPERATOR = "--"*20 +"\n"
class Human(object): # (T)his(I)s(H)uman = 파스칼타입
total_count = 0 # 클래스 변수 = 전체적용
__icon = '무던함' # __icon (성격)
def __init__(self, name): # 매직매서드(생성자)
Human.total_count += 1
self.name = name
def __add__(self, other_obj):
print("'{}'와 '{}'는 결혼했습니다.".format(self.name, other_obj.name))
def __sub__(self, other_obj):
print("'{}'와 '{}'는 이혼했습니다.".format(self.name, other_obj.name))
def __del__(self):
print("'{}'는 죽었습니다.".format(self.name))
def say_hello(self, other_obj): # 기능을 함수(매서드)
print("안녕하세요 '{}'님, 저는 '{}'입니다".format(other_obj.name, self.name))
def say_status(self): # 성격(__icon)을 알려줍니다.
print("'{}' 님의 성격은 '{}' 입니다.".format(self.name, self.__icon))
def set_name(self, modified_name):
print("'{}'님의 이름이 '{}'으로 변경 되었습니다".format(self.name, modified_name))
self.name = modified_name
""" 클래스 매서드는, 인스턴스 명으로 클래스변수에 접근할 방법을 열어준다."""
@classmethod
def set_icon(cls, modified_icon):
print("'{}' ---> '{}' 변경!!".format(cls.__icon, modified_icon))
cls.__icon = modified_icon
""" 스테틱 매서드는, 객체기능과 상관없지만 그림(편의)상 포함시켜야 할 때"""
@staticmethod
def show_shortened_life_story_with(obj1, obj2):
print("\n\n'{}'와 '{}'의 짧은 인생스토리~ 시작!!.."
"\n--------------------".format(obj1.name, obj2.name))
obj1 + obj2 # __add__() 매직매서드 실행
obj1 - obj2 # __Sub__() 매직매서드 실행
obj1.__del__() # del().. __del__ 매직매서드 실행
class ManOne(Human):
def __init__(self, name):
super().__init__(name)
print("'{}'님이 추가 되었습니다. 총 {}명".format(self.name, self.total_count))
kim = ManOne('김철수')
kim.__icon = '영리함' # 사유화가 되었다 = 'self.__icon'이 추가 되었다
# kim.set_icon('영리함') # ManOne의 '빵틀'이 변경되었다.
shin = ManOne('신영희')
shin.__icon = '난폭함' # 사유화가 되었다 = 'self.__icon'이 추가 되었다
# shin.set_icon('난폭함') # ManOne의 '빵틀'이 변경 되었다.
# kim.set_name('김철철철철철') # '빵'이 변경 되었다
# kim.name = '김철철철철철' # '빵'이 변경 되었다
print(SEPERATOR)
# kim.__icon= '--- 좀비스러움 ---' # '빵'이 변경 되었습니다.
kim.set_icon('--- 좀비스러움 ---') # ManOne의 '빵틀'이 변경되었다.
# print('[Human]======', Human._Human_icon)
# print('[ManOne]=====', ManOne._ManOne_icon)
kim.say_status() # '김철수' 님의 성격은 '영리함' 입니다.
kim.say_hello(shin) # 안녕하세요 '신영희'님, 저는 '김철수'
print(SEPERATOR)
shin.say_status() # '신영희' 님의 성격은 '난폭함' 입니다.
shin.say_hello(kim) # '신영희' 님의 성격은 '난폭함' 입니다.
print(SEPERATOR)
kim.show_shortened_life_story_with(kim, shin)
# shin.show_shortened_life_story_with(kim, shin)
# Human.show_shortened_life_story_with(kim, shin)
# ManOne.show_shortened_life_story_with(kim, shin)
print(SEPERATOR)
"""
'김철수'와 '신영희'의 짧은 인생스토리~ 시작!!..
--------------------
'김철수'와 '신영희'는 결혼했습니다.
'김철수'와 '신영희'는 이혼했습니다.
'김철수'는 죽었습니다.
"""
hu = ManOne('무던한인간') # 인스턴스 선언! (인스턴스 생성)
hu1 = ManOne('난폭한인간')
hu1.__icon = '난폭함' # self.__icon 이 추가됨
hu2 = ManOne('사랑스러운인간')
hu2.__icon = '사랑스러움' # self.__icon 이 추가됨
print(SEPERATOR)
hu.say_status()
hu.say_hello(hu1)
print(SEPERATOR)
hu1.say_status()
hu1.say_hello(hu2)
print(SEPERATOR)
print("COUNT: ", hu.total_count) # 선언된 인스턴스. 값 또는 기능() 으로 호출
print("성격: ", hu2.__icon)
hu2.say_status()
hu2.say_hello(hu)
print(SEPERATOR)
def test2():
class ManOne(Human):
def __init__(self, name):
super().__init__(name)
print("'{}'님이 추가되었습니다, 그래서, 총 '{}'명".format(
self.name, super().total_count
))
""" 스테틱 매서드는, 객체기능과 상관없지만 그림(편의)상 포함시켜야 할 때"""
@staticmethod
def show_shortened_life_story_with(obj1, obj2):
print("\n\n'{}'와 '{}'의 짧은 인생스토리~ 시작!!.."
'\n--------------------'.format(obj1.name, obj2.name))
obj1 + obj2 # __add__() 매직매서드 실행
obj1 - obj2 # __Sub__() 매직매서드 실행
obj1.__del__() # del().. __del__ 매직매서드 실행
""" 클래스 매서드는, 인스턴스 명으로 클래스변수에 접근할 방법을 열어준다."""
@classmethod
def show_count(cls):
print("전체 인원은=", cls.total_count)
mo = ManOne('김철수')
m1 = ManOne('신영희')
print(SEPERATOR)
mo.show_shortened_life_story_with(mo, m1)
mo.show_count()
"""
'김철수'와 '신영희'의 짧은 인생스토리~ 시작!!..
--------------------
'김철수'와 '신영희'는 결혼했습니다.
'김철수'와 '신영희'는 이혼했습니다.
'김철수'는 죽었습니다.
전체 인원은= 2
"""
def test1_instance_class():
# mo.icon = "희안함" # ... 클래스 icon 을 바꾼게 아니고 / self.icon 생성
mo.look = "잘생김"
print("철수성격1=", mo.icon)
print("철수외모1=", mo.look)
# m1.icon = "상냥함"
print("영희성격2=", m1.icon)
# print("영희성격2=", m1.look)
ManOne.inner = "----좀비스러움----" # 클래스변수도 외부에서 생성가능
print("ManOne Icon 생성 =", ManOne.inner)
print(mo.inner)
print(m1.inner)
"""
철수성격1= 무던함
철수외모1= 잘생김
영희성격2= 무던함
ManOne Icon 생성 = ----좀비스러움----
----좀비스러움----
----좀비스러움----
"""