做業-python-面向對象編程-使用OOP的思想升級四則運算

# -*- coding:utf-8 -*-
# 3,使用OOP的思想升級四則運算.
class cal():
def __init__(self,sign,num1,num2):
self.sign=sign
self.num1=num1
self.num2=num2
def get_result(self):
if self.sign=='+':
result=self.num1 + self.num2
print('%d加%d等於%d'%(self.num1,self.num2,result))
elif self.sign=='-':
result = self.num1 - self.num2
print('%d減%d等於%d' % (self.num1, self.num2, result))
elif self.sign=='*':
result = self.num1 * self.num2
print('%d乘%d等於%d' % (self.num1, self.num2, result))
elif self.sign=='/':
result = self.num1 / self.num2
print('%d除%d等於%d' % (self.num1, self.num2, result))
return result

a=cal('+',2,3)
a.get_result()

b=cal('-',5,2)
b.get_result()

c=cal('*',3,4)
c.get_result()

d=cal('/',6,3)d.get_result()
相關文章
相關標籤/搜索