zoukankan      html  css  js  c++  java
  • 【Python】面向对象的运算符重载

      面向对象的编程中可以对运算符进行重载,使运算符可以对该类的实例进行操作。

    重载方法的一般格式如下:

    1 def __运算符名__(self, other):
    2   运算符语句

    比较常用的运算符重载有三类:二元算术运算符重载、反向算术运算符重载、比较运算符重载、一元运算符重载

    1 二元算术运算符的重载:
    2 方法名                  运算符和表达式      说明
    3 __add__(self,rhs)        self + rhs      加法
    4 __sub__(self,rhs)        self - rhs      减法
    5 __mul__(self,rhs)        self * rhs      乘法
    6 __truediv__(self,rhs)   self / rhs       除法
    7 __floordiv__(self,rhs)  self //rhs       整除
    8 __mod__(self,rhs)       self % rhs       取模
    9 __pow__(self,rhs)       self **rhs       幂运算
    *rhs的含义是right hand side
    1 反向算术运算符的重载:
      当运算符的左侧为Python的内建数据类型(如:int、float等),右侧为自定义的对象类型时就需要对运算符进行反向重载。
    2 方法名                  运算符和表达式       说明 3 __radd__(self,lhs)       lhs + self       加法 4 __rsub__(self,lhs)       lhs - self       减法 5 __rmul__(self,lhs)       lhs * self       乘法 6 __rtruediv__(self,lhs)   lhs / self       除法 7 __rfloordiv__(self,lhs)  lhs // self       整除 8 __rmod__(self,lhs)       lhs % self       取模 9 __rpow__(self,lhs)       lhs ** self       幂运算
    *lhs的含义是left hand side
    *可见反向算术运算符的符号名就是普通运算符加上r(reverse)
    1 比较算术运算符的重载:
    2 方法名                  运算符和表达式      说明
    3 __lt__(self,rhs)       self < rhs        小于
    4 __le__(self,rhs)       self <= rhs       小于等于
    5 __gt__(self,rhs)       self > rhs        大于
    6 __ge__(self,rhs)       self >= rhs       大于等于
    7 __eq__(self,rhs)       self == rhs       等于
    8 __ne__(self,rhs)       self != rhs       不等于
    1 一元运算符的重载
    2 方法名              运算符和表达式        说明
    3 __neg__(self)         - self           负号
    4 __pos__(self)         + self           正号
    5 __invert__(self)      ~ self           取反
     1 例:
        以分数类为例对运算符进行重载。
    2 class Fraction: # 分数类 3 def __init__(self, num, deno): 4 if deno < 0: 5 deno = -1 * deno 6 num = -1 * num 7 h = hcf(num, deno) 8 self.num = int(num/h) 9 self.deno = int(deno/h) 10 11 def __add__(self, other): # 加法 12 temp_lcm = lcm(self.deno, other.deno) 13 num = self.num*(temp_lcm/self.deno)+other.num*(temp_lcm/other.deno) 14 deno = temp_lcm 15 return Fraction(num, deno) 16 17 def __sub__(self, other): # 减法 18 temp_lcm = lcm(self.deno, other.deno) 19 num = self.num * (temp_lcm / self.deno) - other.num * (temp_lcm / other.deno) 20 deno = temp_lcm 21 return Fraction(num, deno) 22 23 def __mul__(self, other): # 乘法 24 return Fraction(self.num*other.num, self.deno*other.deno) 25 26 def __truediv__(self, other): # 除法 27 return Fraction(self.num*other.deno, self.deno*other.num) 28 29 def __rtruediv__(self, lhs): # 反向运算符重载 30 return Fraction(lhs*self.deno, self.num) 31 32 def reverse(self): # 取倒数 33 return Fraction(self.deno, self.num) 34 35 def prtfloat(self): # 以浮点数形式输出 36 print(format(self.num/self.deno, '.1f')) 37 38 def prt(self): # 以分数形式输出 39 print('{}/{}'.format(self.num, self.deno))

    参考链接:

    https://blog.csdn.net/zhangshuaijun123/article/details/82149056

  • 相关阅读:
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_发布者策略控制
    删除Windows服务
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_高级管理控制(配置)
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_运行时解析类型引用
    SQL-Error-1
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_.Net Framework 部署目标
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_.Net Framework 部署目标(一)
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_NGen.exe
    Number对象
    css content
  • 原文地址:https://www.cnblogs.com/protectione055/p/11816759.html
Copyright © 2011-2022 走看看