参考代码1
- class Stu:
- def __init__(self,name,weight):
- self.__name=name
- self.__weight=weight
-
- @property
- def Name(self):
- return self.__name
-
- @property
- def Weight(self):
- return self.__weight
-
- def __add__(self,obj):
- sN=self.Name+" "+obj.Name
- sW=self.Weight+obj.Weight
- newS=Stu(sN,sW)
- return newS
-
- def __str__(self):
- print("Name:"+self.__name+" Weight:"+str(self.__weight))
- return ""
-
- s1=Stu("张三",100)
- s2=Stu("李四",80)
- s3=s1+s2
- print(s1)
- print(s2)
- print(s3)
- print(s3.Name)
- print(s3.Weight)
- print(s3)
参考代码2:
