类例1

来源:互联网 发布:官方软件下载大全 编辑:程序博客网 时间:2024/05/22 10:51
class sclass()    def display(self):        print('current value=%s'% self.data)class class3(sclass):    #初始化属性值    def __init__(self,value):        self.data=value    #"+"运算符重载    def __add__(self,other):        return class3(self.data+other)    #打印对象    def __str__(self):        return 'class3:%s'% self.data    def mul(self,other):        self.data*=othera=class3("abc")a.display()print(a)   #调用__str__方法b=a+" xyz" #调用__add__方法b.display()print(b)a.mul(3)print(a)

0 0
原创粉丝点击