zoukankan      html  css  js  c++  java
  • 重载运算符强化2-返回值

    '''
    重载运算符强化--返回值
    '''
    
    # print(1+2)#不同的类型有不同的解释
    # print('1'+'2')
    
    class complex:
        def __init__(self,x,y):
            self.x=x
            self.y=y
        def show(self):
            print(self.x,'+',self.y,'i')
        def __add__(self, other):#重载的含义就是针对本类型,对+ 重新解释
            if type(other)==type(self):
                return complex(self.x+other.x,self.y+other.y)#加法的返回值
            elif type(other)==type(10):
                return complex(self.x + other,self.y + other)
    
    
    
    
    
    c1=complex(1,2)
    
    c2=complex(3,5)
    
    c1.show()
    c2.show()
    
    c3=c1+c2
    c4=c1.__add__(c2)#效果同c3
    c5=c1+10
    c1.show()
    c2.show()
    c3.show()
    c4.show()
    c5.show()
    
    '''
    1 + 2 i
    3 + 5 i
    1 + 2 i
    3 + 5 i
    4 + 7 i
    4 + 7 i
    11 + 12 i
    '''
  • 相关阅读:
    Shortest Paths
    Minimum Spanning Trees
    Programming Assignment 1: WordNet
    Directed Graphs
    内连接
    数据中台文档--参考
    ORACLE触发器字段触发
    DTS
    oracle删除归档日志
    OGG 分区表
  • 原文地址:https://www.cnblogs.com/liangliangzz/p/11350294.html
Copyright © 2011-2022 走看看