在Python中从类外部调用方法

时间:2017-09-22 13:36:07

标签: python class

这是我一直在制作的矢量类,但是,当我尝试运行它时,它会出现以下错误:“NameError:name'self'未定义”

如果有人能告诉我出了什么问题,那将非常感激。

我是Python的新手,所以对于如何改进/优化代码的任何反馈也都很受欢迎。

class Vector(object):

    @staticmethod
    def __init__(self,x,y):
        self.x = x
        self.y = y


    @staticmethod   
    def __add__(self, newVector):
        return Vector(self.x+newVector.x, self.y+newVector.y)


    @staticmethod
    def __subtract__(self,newVector):
        return Vector(self.x-newVector.x, self.y-newVector.y)


    @staticmethod
    def __dotProduct__(self, newVector):
        return (self.x*other.x) + (self.y*other.y)


    @staticmethod
    def __length__(self, newVector):
        return sqrt((self.y)^2 + (self.x)^2)


    @staticmethod
    def __xTheta__ (self, Vector):
        return math.atan(math.degrees(self.y/self.x))


    @staticmethod
    def __yTheta(self, Vector):
        return math.atan(math.degrees(self.x/self.y))


    @staticmethod
    def __resolveX__(self, Vector):
        return Vector(self.x, 0)


    @staticmethod
    def __resolveY__(self, Vector):
        return Vector(0, self.y)


    @staticmethod
    def __scalarProduct__(self, scalar):
        return Vector(self.x*scalar, self.y * scalar)


    @staticmethod
    def __normalise__(self, Vector):
        return Vector(self.x/self.length(Vector),self.y/self.length(Vector))


a = Vector(3,4)
print(a.length())

0 个答案:

没有答案