-
Notifications
You must be signed in to change notification settings - Fork 0
/
220208.py
175 lines (141 loc) · 2.76 KB
/
220208.py
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
#클래스의 원리
result = 0
def add(num):
global result
result += num
return result
print(add(6))
print(add(7))
result1 = 0
result2 = 0
def add1(num):
global result1
result1 += num
return result1
def add2(num):
global result2
result2 += num
return result2
print(add1(3))
#3
print(add2(5))
#5
print(add1(9))
#12
print(add2(8))
#13
#객체끼리는 서로 독립적임.
#사칙연산 맛보기
class Cal:
def __init__(self):
self.result = 0
def add(self, num):
self.result += num
return self.result
cal1 = Cal()
cal2 = Cal()
print(cal1.add(3))
#3
print(cal2.add(5))
#5
print(cal1.add(9))
#12
print(cal2.add(8))
#13
#사칙연산 계산기
class fourCal:
def setdata(self, first, second):
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def mul(self):
result = self.first * self.second
return result
a = fourCal()
a.setdata(100,78)
print(a.first)
print(a.second)
b = fourCal()
b.setdata(56,99)
print(b.first)
print(b.second)
print(id(a.first))
#2477478972752
print(id(b.first))
#2477478971344
print(a.add())
#178
print(b.add())
#155
print(a.mul())
#7800
print(b.mul())
#5544
#사칙연산 계산기
class FourCal:
def __init__(self, first, second):
#__init__ == 생성자(Constructor), 객체가 생성될 때 자동으로 호출됨.
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def mul(self):
result = self.first * self.second
return result
def div(self):
result = self.first / self.second
return result
a = FourCal(5,7)
#setdata 없이도 입력값 넣을 수 있음.
print(a.add())
print(a.mul())
#클래스 상속
#class 새로운 클래스 이름(상속할 클래스 이름)
class MoreFourCal(FourCal):
def pow(self):
result = self.first ** self.second
return result
a = MoreFourCal(5,2)
print(a.pow())
#25
print(a.add())
#7
print(a.mul())
#10
#매서드 오버라이딩
#함수 내용 덮어 쓰기 가능
class SafeFourCal(MoreFourCal):
def div(self):
if self.second == 0:
return "Error"
else:
return self.first / self.second
a = SafeFourCal(7,0)
print(a.div())
#Error
b=SafeFourCal(12,4)
print(b.div())
#3.0
# mod 1.py
#def add(a,b):
# return a+b
#def mul(a,b):
# return a*b
#if __name__ == "__main__":
# print(add(6,7))
# 13
# print(mul(6,7))
# 42
#자동으로 디렉터리가 이동되는 듯
import mod1
print(mod1.add(3,4))
print(mod1.mul(3,4))
from mod1 import add
print(add(3,4))
from mod1 import *
print(add(5,6))
print(mul(5,6))
#13, 42출력 없이 실행됨.