Python 构造一个类

Python是一门面向对象的编程语言,类的定义和构造很重要。

以下为一个自定义分数类:

#Python编程范式是面对对象,下面为用户自定义一个分数类。
#这个类包含了分数的加减乘除运算和逻辑运算,较为完整。
class Fraction:
    def __init__(self,top,bottom):
        self.num = top
        self.den = bottom
    def show(self):
        print(self.num,'/',self.den,sep='')
    def __str__(self):         #若无此函数,直接执行print输出对象会报错。
        return str(self.num)+'/'+str(self.den)
    def __add__(self,other): #双目运算符+,self指向第一个运算对象,otherfaction指向另一个对象。
        new_num = self.num * other.den + self.den * other.num
        new_den = self.den * other.den
        common = gcd(new_num,new_den)
        return Fraction(new_num/common,new_den/common)
    def __sub__(self,other): #减法
        new_num = self.num * other.den - self.den * other.num
        new_den = self.den * other.den
        common = gcd(new_num,new_den)
        return Fraction(new_num/common,new_den/common)
    def __mul__(self,other): #乘法
        new_num = self.num * other.num
        new_den = self.den * other.den
        common = gcd(new_num,new_den)
        return Fraction(new_num/common,new_den/common)
    def __truediv__(self,other): #除法
        new_num = self.num * other.den
        new_den = self.den * other.num
        common = gcd(new_num,new_den)
        return Fraction(new_num/common,new_den/common)
    def __eq__(self,other):  #判断相等的函数重写,类似c++重载相等运算符
        new_num1 = self.num * other.den
        new_num2 = other.num * self.den
        return new_num1 == new_num2
    def __le__(self,other):   #<=
        new_num1 = self.num * other.den
        new_num2 = other.num * self.den
        return new_num1 <= new_num2
    def __ge__(self,other):  #>=
        new_num1 = self.num * other.den
        new_num2 = other.num * self.den
        return new_num1 >= new_num2
    def __lt__(self,other):   #<
        new_num1 = self.num * other.den
        new_num2 = other.num * self.den
        return new_num1 < new_num2
    def __gt__(self,other):  #>
        new_num1 = self.num * other.den
        new_num2 = other.num * self.den
        return new_num1 > new_num2
    def __ne__(self,other):  #!=
        new_num1 = self.num * other.den
        new_num2 = other.num * self.den
        return new_num1 != new_num2
def gcd(m,n):        #最大公因数算法,使用欧几里得算法。化简分数
        while m % n != 0:
            oldm = m
            oldn = n
            n = oldm
            m = oldm % oldn
        return n
#建立一些数据用于测试函数。
my_f0 = Fraction(1,2)
my_f1 = Fraction(1,2)
my_f2 = Fraction(-1,2)
my_f3 = Fraction(1,3)
my_f4 = Fraction(2,3)
print(my_f1)   #测试输出函数
my_f5 = my_f1 + my_f2
print(my_f5)
my_f6 = my_f4 - my_f3
print(my_f6)
my_f7 = my_f1 * my_f4
print(my_f7)
my_f8 = my_f2 / my_f3
print(my_f8)
print(my_f1 > my_f2,'== 1')
print(my_f1 < my_f2,'== 0')
print(my_f1 >= my_f1,'== 1')
print(my_f1 >= my_f4,'== 0')
print(my_f1 <= my_f2,'== 0')
print(my_f3 <= my_f4,'== 1')
print(my_f1 == my_f0,'== 1')
print(my_f1 != my_f3,'== 1')

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Math-L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值