Python language-center of a circle using OOP

前端 未结 3 1626
悲哀的现实
悲哀的现实 2021-01-28 07:33
class Point:

    def __init__(self, initX, initY):
        \"\"\" Create a new point at the given coordinates. \"\"\"
        self.x = initX
        self.y = initY

            


        
3条回答
  •  孤独总比滥情好
    2021-01-28 08:14

    Both getX and getY are methods in your code, not attributes. So you will need to call them using getX() and getY().

    So ma=(p2.getY-p1.getY)/(p2.getX-p1.getX) becomes:

    ma = (p2.getY()-p1.getY())/(p2.getX()-p1.getX())
    

    And so on, the other code changes.

    Otherwise, you can also define your methods as @property:

    class Point:
        ...
        ...
        @property
        def getX(self):
            return self.x
        @property
        def getY(self):
            return self.y
        ...
    

    And now you can access these as p1.getX and p2.getY and so on.

    Note that the above @property decorator turns the method into a getter, which makes sense to use only with private variables (variables defined to start with _).

    As such, since both x and y are normal attributes of your class, you can access them directly without using and property decorators or using getter methods, like p1.x and p2.y, as @Padraic points in his post.

提交回复
热议问题