"""
클래스 상속 과 클래스 변수
"""
class Calculator:
def __init__(self, first, second): #생성자
self.first = first
self.second = second
def add(self): #add메소드
result = self.first + self.second
return print(result)
def substraction(self): #substraction메소드
result = self.first - self.second
return print(result)
def multiplication(self): #multiplication메소드
result = self.first * self.second
return print(result)
def division(self): #division메소드
result = round((self.first / self.second),5)
return print(result)
# 클래스 상속 _ UpdateCalculator : 자식 클래스
class UpdateCalculator(Calculator):
def __init__(self, first, second):
super().__init__(first, second)
def division(self): #division메소드
result = self.first // self.second
return print(result)
#입려과 출력
while True:
print("="*4+"클래스 상속"+"="*4)
print("1.덧셈 2.뺄셈 3.곱셉 4.나눗셈 5.클래스 변수 0.종료")
option = int(input("번호를 선택하세요 : "))
if option == 0:
print("종료합니다.")
break
if option < 5 and option >= 1:
num1,num2 = map(int, input("숫자를 입력해주세요(ex: 1,2) : ").split(","))
number = Calculator(num1,num2)
update_number = UpdateCalculator(num1,num2)
if option == 1:
print("Calculator : ")
number.add()
print("자식 Calculator : ")
update_number.add()
elif option == 2:
print("Calculator : ")
number.substraction()
print("자식 Calculator : ")
update_number.substraction()
elif option == 3:
print("Calculator : ")
number.multiplication()
print("자식 Calculator : ")
update_number.multiplication()
else:
print("Calculator : ")
number.division()
print("자식 Calculator : ")
update_number.division()
if option == 5:
class class_variable():
favorite = "도깨비"
drama = class_variable()
a = class_variable()
print("처음 대표 클래스 변수 : ",drama.favorite, " a 클래스 변수 : ",a.favorite)
print("처음 클래스 변수 메모리값 : ",id(drama.favorite), " a 클래스 변수 메모리값 : ",id(a.favorite))
class_variable.favorite = input("좋아하는 드라마는? : ")
print("바뀐 클래스 변수 : ",drama.favorite, " 바뀐 a 클래스 변수 : ",a.favorite)
print("바뀐 클래스 변수 메로리값 : ",id(drama.favorite), " 바뀐 a 클래스 변수 메모리값 : ",id(a.favorite))
>> 결과
'코린이_탈출 > 파이썬 기본 문법' 카테고리의 다른 글
[모각코_파이썬기초문법] 클래스의 상속 (0) | 2021.01.28 |
---|---|
[모각코_파이썬기초문법] 클래스와 생성자-과제 (1) | 2021.01.27 |
[모각코_파이썬기초문법] 클래스와 생성자 (0) | 2021.01.27 |
[모각코_파이썬기초문법] 튜플, 집합, 딕셔너리 (0) | 2021.01.26 |
[모각코_파이썬기초문법] 리스트 (0) | 2021.01.25 |