题解 | #重载运算#

重载运算

https://www.nowcoder.com/practice/342d1b8b0fe3416797bad62d22cbb51a

class Coordinate:

    def __init__(self, x, y):

        self.x = x

        self.y = y

    def __str__(self):

        print(f'({self.x}, {self.y})')

    def __add__(self,cd): # 传入一个新的对象,分别将x与y坐标分别相加

        self.x += cd.x

        self.y += cd.y

def main():

    x1, y1 = list(map(int, input().split())) # 利用map函数和list函数将输入字符串转化为整数型,并迭代输出,传给x1,x2

    x2, y2 = list(map(int, input().split()))

    c1 = Coordinate(x1, y1)

    c2 = Coordinate(x2, y2)

    c1.__add__(c2)

    c1.__str__()

main()

全部评论

相关推荐

03-18 01:22
门头沟学院 Java
肖先生~:先别说工资,现在有个工作就不错了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务