zoukankan      html  css  js  c++  java
  • python-重载

    重载概念
      重载是对继承的父类方法进行重新定义。重载可以重新定义方法还可以重新定义运算符。因为通过继承的类不一定能满足当前类的需求。在当前类中只需要修改部分内容而达到自己的需求。

    重载特点
      减少代码量和灵活指定型类


      子类具有父类的方法和属性
      子类不能继承父类的私有方法或属性


      子类可以添加新的方法


      子类可以修改父类的方法

    方法重载

     1 # coding=utf-8
     2 
     3 class human(object):
     4     __name = ''  # 定义属性
     5     __sex = 0
     6     __age = 0
     7     __height = 0
     8     __weight = 0
     9 
    10     def __init__(self, sex, age, height, weight):
    11         self.__sex = sex
    12         self.__age = age
    13         self.__height = height
    14         self.__weight = weight
    15 
    16     def set_name(self,name):
    17         self.__name = name
    18 
    19     def show(self):
    20         print(self.__name, self.__sex, self.__age, self.__height, self.__weight)
    21 
    22 
    23 class student(human):
    24     __classes = 0
    25     __grade = 0
    26     __num = 0
    27     def __init__(self, classes,grade,num,sex,age,height,weight):  # 重载 __init__ 方法
    28         self.__classes = classes
    29         self.__grade = grade
    30         self.__num = num
    31         human.__init__(self, sex, age, height, weight)
    32 
    33     def show(self):  # 重载 show 方法
    34         human.show(self)
    35         print(self.__classes,self.__grade,self.__num)
    36 
    37 
    38 if __name__ == '__main__':
    39     a = student(3,2,19910218,'',13,'158',45)
    40     a.set_name('小明')
    41     a.show()

    运行结果

       

    运算符重载
      运算符重载是在类方法中拦截内置的操作——当类的实例出现在内置操作中,Python自动调用重新定义的方法,并将重新定义方法的返回值变成了相应操作的结果   

     1 class List(object):
     2     __list = []
     3 
     4     def __init__(self, *args):   # 重载 __init__ 方法
     5         self.__list = []
     6         for arg in args:
     7             self.__list.append(arg)
     8 
     9     def __add__(self, n):      # 重载 + 运算符
    10         for i in range(0,len(self.__list)):
    11             self.__list[i] = self.__list[i] + n
    12 
    13     def __sub__(self, n):      # 重载 - 运算符
    14         for i in range(0,len(self.__list)):
    15             self.__list[i] = self.__list[i] - n
    16 
    17     def __mul__(self, n):      # 重载 * 运算符
    18         for i in range(0,len(self.__list)):
    19             self.__list[i] = self.__list[i] * n
    20 
    21     def __div__(self, n):      # 重载 / 运算符
    22         for i in range(0,len(self.__list)):
    23             self.__list[i] = self.__list[i] / n
    24 
    25     def show(self):
    26         print(self.__list)
    27 
    28 if __name__ == '__main__':
    29     myList = List(1,2,3,4,5,6,7,8,9)
    30     myList.show()
    31 
    32     myList + 3
    33     myList.show()
    34 
    35     myList - 2
    36     myList.show()
    37 
    38     myList * 2
    39     myList.show()  

      运行结果

      

    常见的运算符重载方法

    method overload call
    __init__ 构造函数 对象创建: X = Class(args)
    __del__ 析构函数 X对象收回
    __add__ 云算法+ 如果没有_iadd_, X+Y, X+=Y
    __or__ 运算符| 如果没有_ior_,X|Y, X|=Y
    _repr__, __str__ 打印,转换 print(X),repr(X),str(X)
    __call__ 函数调用 X(*args, **kwargs)
    __getattr__ 点号运算 X.undefined
    __setattr__ 属性赋值语句 X.any=value
    __delattr__ 属性删除 del X.any
    __getattribute__ 属性获取 X.any
    __getitem__ 索引运算 X[key],X[i:j]
    __setitem__ 索引赋值语句 X[key],X[i:j]=sequence
    __delitem__ 索引和分片删除 del X[key],del X[i:j]
    __len__ 长度 len(X),如果没有__bool__,真值测试
    __bool__ 布尔测试 bool(X)
    __lt__, __gt__, 
    __le__, __ge__, 
    __eq__, __ne__
    特定的比较 X<Y,X>Y,X<=Y,X>=Y, 
    X==Y,X!=Y 
    注释:(lt: less than, gt: greater than, 
      le: less equal, ge: greater equal, 
      eq: equal, ne: not equal 
    __radd__ 右侧加法 other+X
    __iadd__ 实地(增强的)加法 X+=Y(or else __add__)
    __iter__, __next__ 迭代环境 I=iter(X), next()
    __contains__ 成员关系测试 item in X(任何可迭代)
    __index__ 整数值 hex(X), bin(X),  oct(X)
    __enter__, __exit__ 环境管理器 with obj as var:
    __get__, __set__,
    __delete__
    描述符属性 X.attr, X.attr=value, del X.attr
    __new__ 创建 在__init__之前创建对象

    原文发布在 软件羊皮卷 微信公众号中,欢迎大家关注

  • 相关阅读:
    冒泡排序
    跑马(行转列,列转行)
    选择排序
    day06-迭代器
    day05-装饰器作业
    day07-生成器
    day08-内置函数和匿名函数
    day09-正侧表达式
    144-SpringBoot的编码问题?
    143-SprinBoot如何使用Servlet?
  • 原文地址:https://www.cnblogs.com/tynam/p/10359201.html
Copyright © 2011-2022 走看看