''' 클래스 '''
class Calculator:
def __init__(self):
self.result = 0
def add(self,num):
self.result += num
return self.result
cal = Calculator()
print(cal.add(3))
print(Calculator().add(3))
class FourCal:
def setdata(self,first,second): # 변수를 저장하기 위해서 setdata를 사용한다.
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def sub(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()
b = FourCal()
a.setdata(4,2)
b.setdata(2,5)
print(a.add())
print(a.sub())
print(a.mul())
print(a.div())
print(b.add())
print(b.sub())
print(b.mul())
print(b.div())
''' 생성자 '''
class FourCal:
def __init__(self,first,second): # 변수를 저장하기 위해서 __init__(생성자)를 사용한다.
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def sub(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(4,2)
print(a.add())
print(a.sub())
print(a.mul())
print(a.div())
''' 클래스 상속 '''
class FourCal:
def __init__(self,first,second):
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def sub(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(4,2)
print(a.add())
print(a.sub())
print(a.mul())
print(a.div())
class MoreFourCal(FourCal): # 상속은 기존 클래스에서 기능을 확장할 때 사용한다.
def pow(self):
result = self.first ** self.second
return result
a=MoreFourCal(4,2)
print(a.add())
print(a.mul())
print(a.sub())
print(a.div())
print(a.pow())