python数据结构1:类定义

类定义

class Rational0:
    def __init__(self,num,den=1):
        self.num = num
        self.den = den

    def plus(self,another):
        den = self.den * another.den
        num = (self.num * another.den + self.den * another.num)
        return Rational0(num,den)

    def print(self):
        print(str(self.num)+'/'+str(self.den))

r1 = Rational0(3,5)
r2 = r1.plus(Rational0(7,5))
r2.print()

你可能感兴趣的:(数据结构)