• python 运算符重载__add__


    print(1 + 2)
    print("1" + "2")
    #不同的类型用加法会有不同的解释

    class Person(object):
    def __init__(self, num):
    self.num = num
    #运算符重载
    def __add__(self, other):
    return Person(self.num + other.num)
    def __str__(self):
    return "num = " + str(self.num)
    per1 = Person(1)
    per2 = Person(2)
    print(per1 + per2)#per1 + per2 ==== per1.__add__(per2)
    #print(per1.__add__(per2))
    print(per1)
    print(per2)
  • 相关阅读:
    数组
    对象
    js继承
    js原型原型链
    es6(初级)
    canvas背景
    Angular.js进阶
    Angular.js-2入门
    angular.js-1初识
    js之广告
  • 原文地址:https://www.cnblogs.com/pygo/p/12326415.html
Copyright © 2020-2023  润新知