创建一个python类来查找直线的斜率和长度

2024-06-25 23:03:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要弄清楚如何创建一个类来查找线段的斜率和长度,并传递两个元组来表示端点(x,y)。我的问题是,当我试图创建一个segment对象时,它说int对象是不可调用的。请帮忙

class Segment():
    def __init__(self, tup1, tup2):
            self.x1 = tup1[0]
            self.x2 = tup2[0]
            self.y1 = tup1[1]
            self.y2 = tup2[1]
            self.slope = 0
            self.length = 0

    def length(self):
            self.length = math.sqrt((y2-y1)**(2+(x2-x1)**2))
            print(self.length)

    def slope(self):
            self.slope = ((y2-y1)/(x2-x1))
            print(self.slope)

Tags: 对象selfdeflengthslope元组printx1
3条回答

发生这种情况是因为您正在用构造函数中的0覆盖self.length方法。所以当你试图调用s.length()时,它实际上是在试图调用0(),因为你分配了self.length = 0。你知道吗

您可能应该这样做(注意我在每个x和y值前面加上self,这样它就使用了实例的属性值):

class Segment():
    def __init__(self, tup1, tup2):
        self.x1 = tup1[0]
        self.x2 = tup2[0]
        self.y1 = tup1[1]
        self.y2 = tup2[1]

        self.length = math.sqrt((self.y2-self.y1)**(2+(self.x2-self.x1)**2))
        self.slope = ((self.y2-self.y1)/(self.x2-self.x1))

然后只需访问实例属性即可访问lengthslope属性:

>>> s = Segment((1,2),(3,4))
>>> s.length
8.0
>>> s.slope
1

(同样值得注意的是,您的length函数不太正确,但我将把这个修正留给您!)你知道吗

在代码中:

class Segment():
    def __init__(self, tup1, tup2):
            self.x1 = tup1[0]
            self.x2 = tup2[0]
            self.y1 = tup1[1]
            self.y2 = tup2[1]
            self.slope = 0 ### Here
            self.length = 0 ### And here

使Segment类实例的lenght变量等于0(int),slope变量等于0。例如现在:

line = Segment((0,0),(1,1))
line.lenght # 0
line.slope # 0

你知道吗线路长度以及直线斜率不再是行变量的函数。它们是int。int是不可调用的。必须更改函数或变量的名称。也可以通过self.x1、self.y1等更改yorlenghtslope函数中的xy变量。尝试:

class Segment():
    def __init__(self, tup1, tup2):
            self.x1 = tup1[0]
            self.x2 = tup2[0]
            self.y1 = tup1[1]
            self.y2 = tup2[1]
            self.slope = 0
            self.length = 0

    def Length(self):
            self.length = math.sqrt((self.y2-self.y1)**(2+(self.x2-self.x1)**2))
            print(self.length)

    def Slope(self):
            self.slope = ((self.y2-self.y1)/(self.x2-self.x1))
            print(self.slope)

但是如果你想要一个更好的代码,我会给你两种方法。 如果线的位置不变,请使用:

class Segment():
    def __init__(self, tup1, tup2):
            self.x1 = tup1[0]
            self.x2 = tup2[0]
            self.y1 = tup1[1]
            self.y2 = tup2[1]
            self.length = math.sqrt((self.y2-self.y1)**(2+(self.x2-self.x1)**2)) 
            self.slope = ((self.y2-self.y1)/(self.x2-self.x1))

如果线的位置可能改变,请使用:

class Segment():
    def __init__(self, tup1, tup2):
        self.set_points(tup1,tup2)

    def set_points(self,tup1,tup2):
        self._x1 = tup1[0]
        self._x2 = tup2[0]
        self._y1 = tup1[1]
        self._y2 = tup2[1]
        self.length = math.sqrt((self._y2-self._y1)**(2+(self._x2-self._x1)**2)) 
        self.slope = ((self._y2-self._y1)/(self._x2-self._x1))

我认为你的长度函数不是真的。您可以将Lenght函数替换为:

def Length(self):
        return math.sqrt((self.x1+self.x2)**2+(self.y1+self.y2)**2)

问题是属性和方法的名称相同。如果您想访问长度像instance.length而不是instance.length(),只需更改方法

def length(self):
    self.length = math.sqrt((y2-y1)**(2+(x2-x1)**2))
    print(self.length)

@property
def length(self):
    return math.sqrt((self.y2 - self.y1)**2 + (self.x2 - self.x1)**2)

相关问题 更多 >