# 法,赋予n1和n2初始值,再为该类定义加(addition)、减(subtration)、乘 # (multiplication)、除(division)等公有对象方法,分别对两个属性执行加、减、 # 乘、除的运算。 # b. 创建Number类的对象,调用各个方法,并显示计算结果。
解决方法如下
class Number:
n1 = 1
n2 = 1
def __init__(self,n1_val,n2_val):
self.n1 = n1_val
self.n2 = n2_val
def addition(self): #加
print(self .n1,'+',self .n2,'=',self.n1 + self.n2)
def subtration(self): #减
print(self .n1,'-',self .n2,'=',self.n1 - self.n2)
def multiplication(self): #乘
print(self .n1,'*',self .n2,'=',self.n1 * self.n2)
def division(self): #除
print(self .n1,'/',self .n2,'=',self.n1 / self.n2)
n = Number (10,3)
n.n1 = int(input('请输入第一个数') )
n.n2 = int(input('请输入第二个数') )
n.addition()
n.subtration()
n.multiplication()
n.division()
这个就很简单,就是最基本的,没什么难点,如果不会就多练!!!