如何解决如何将一个类方法的值返回到同一类的其他方法
下面是我的班级,我想把函数add()中n和d的值返回给函数simple(),以便在计算GCD后返回分数,我该怎么做?此外,我不能只在简化中调用 add 函数,因为我将不得不为所有其他计算添加函数 代码如下:
class Fraction:
def __init__(self,num,denom) -> None:
self.num = num
self.denom = denom
if self.denom == 0:
raise ZeroDivisionError("cannot divide by zero")
def __add__(self,other: "Fraction") -> "Fraction":
n: int = self.num * other.denom + self.denom * other.num
d: int = self.denom * other.denom
def simplify(self) -> "Fraction":
n1: Fraction = abs(self.num)
n2: Fraction= abs(self.denom)
gcd: int = 1
k:int = 1
while k <= n1 and k <= n2:
if n1 % k == 0 and n2 % k == 0:
gcd = k
k += 1
a:int = self.num / gcd
b:int = self.denom / gcd
new = Fraction(a,b)
return new
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。