-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_d06_class02.py
More file actions
61 lines (44 loc) · 1.76 KB
/
Copy pathpython_d06_class02.py
File metadata and controls
61 lines (44 loc) · 1.76 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
CUR_YEAR = 2021 ## 전역변수
class Date(object):
def __init__(self, year, month):
self.year = year
self.month = month
print('Date __init__ ')
def disp_Date(self):
print('{}년{}월'.format(self.year, self.month))
def __del__(self):
print('Date __del__ ')
def disp_Date(self):
print('{}년{}월'.format(self.year, self.month))
def __del__(self):
print('Date __del__ ')
class Man(Date): ## Date class를 상속 받는다.
cnt = 0 ## class변수
@staticmethod # 장식자
def get_cnt1():
return Man.cnt
@classmethod
def get_cnt2(cls):
return Man.cnt
## 객체 생성시 자동 호출(constructor)되는 함수, 생성자
def __init__(self, name, year, month):
super().__init__(year, month)
self.name = name
Man.cnt += 1
self.age = CUR_YEAR - year + 1
## 객체 소멸시 자동 호출(destructor)되는 함수, 소멸자
def __del__(self):
Man.cnt -= 1
super().__del__()
print(' Man __del__ ')
## 객체 출력시 자동으로 호출되는 함수, 기본값은 객체의 id값 리턴
def __str__(self):
return '{}님은 {}살'.format(self.name, self.age)
def disp_Man(self):
print('{}님은 {}살'.format(self.name, self.age))
## Date.disp_Date(self) ## Date class의 메서드 호출
super().disp_Date() ## Date class의 메서드 호출
m1 = Man('손흥민', 1992, 7)
m2 = Man('이강인', 2001, 2)
m1.disp_Man()
[출처] AII - 6일차 (양주종의 코딩스쿨 ▶ C언어 ·파이썬 ·리눅스 ·정보처리기사) | 작성자 양주종