class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x,self.y + other.y)
def output(self):
print(self.x,self.y)
class Point3:
def __init__(self,x,y,z):
self.x = x
self.y = y
self.z = z
def __add__(self, other):
return Point3(self.x + other.x,self.y + other.y,self.z + other.z)
def output(self):
print(self.x,self.y,self.z)
def myadd(a,b): #只要有实现add方法的类都可以将实例传递给myadd
return a + b
if __name__ == '__main__':
myadd(Point(1,2),Point(3,4,)).output()
myadd(Point3(1,2,3),Point3(4,5,6)).output()
python_多态
最新推荐文章于 2022-05-12 17:21:36 发布