Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何用两个点(左上角点(0,0)和右下角点(1,1))初始化矩形类?_Python - Fatal编程技术网

在python中,如何用两个点(左上角点(0,0)和右下角点(1,1))初始化矩形类?

在python中,如何用两个点(左上角点(0,0)和右下角点(1,1))初始化矩形类?,python,Python,我希望能够创建一个矩形,其中有两个点的数据属性定义一个矩形的两个相对角,使用上面定义的点,而不是使用继承。但是,我在矩形类中的初始化方法和一些方法方面遇到了问题,我不确定我是否以正确的方式进行操作。我想要初始化:用默认值p1=(0,0),p2=(1,1)初始化 以下是到目前为止我对Point课程的了解: import math class Point: def __init__(self, x: float = 0.0, y: float = 0.0)->None:

我希望能够创建一个矩形,其中有两个点的数据属性定义一个矩形的两个相对角,使用上面定义的点,而不是使用继承。但是,我在矩形类中的初始化方法和一些方法方面遇到了问题,我不确定我是否以正确的方式进行操作。我想要初始化:用默认值p1=(0,0),p2=(1,1)初始化

以下是到目前为止我对Point课程的了解:

import math

class Point:

    def __init__(self, x: float = 0.0, y: float = 0.0)->None:
        self.x = x # initialize to 0
        self.y = y # initialize to 0


    def moveIt(self, dx: float, dy: float)-> None:
        self.x = self.x + dx
        self.y = self.y + dy 

    def distance(self, otherPoint: float):
        if isinstance(otherPoint, Point):
            x1 = self.x
            y1 = self.y
            x2 = otherPoint.x
            y2 = otherPoint.y

            return ( (x1 - x2)**2 + (y1 - y2)**2 )**0.5
当我创建一个点时,这一切似乎都能正常工作

p1 = Point()

print(p1.x, p1.y)

>>>> 0 0
但是,当我创建一个空白矩形对象时,我的矩形类不起作用。代码如下:

class Rectangle:
    def __init__(self, topLeft, bottomRight):
        self.topLeft = 0,0
        self.bottomRight = 1,1
我似乎找不到一种方法来像我在Point类中那样,在这个类中,点从get-go初始化为x=0,y=0。在Rectangle类中有什么方法可以做到这一点吗?我尝试了以下方法,但不允许:

Class Rectangle:
    def __init__(self, topLeft = (0,0), bottomRight = (1,1)):
        self.topLeft = topLeft
        self.bottomRight = bottomRight
当我运行代码时,我得到了一些错误,我不能像这样初始化

r1 = Rectangle()

print(r1.topLeft, r1.bottomRight)
在初始化之后,我希望能够传递我所做的点


最后,我试图从中创建两个方法Get_area以浮点值的形式返回矩形的面积,Get_Permiture以浮点值的形式返回周长。

问题是您编写了
Class
而不是
Class
。您的代码在得到纠正后工作正常

你写了

Class Rectangle:
你应该写作

class Rectangle:
完成:

class Rectangle:
    def __init__(self, topLeft = (0,0), bottomRight = (1,1)):
        self.topLeft = topLeft
        self.bottomRight = bottomRight

r1 = Rectangle()
print(r1.topLeft, r1.bottomRight)    
> (0, 0) (1, 1)
使用以下方法覆盖默认值

r1 = Rectangle(topLeft = (0,0.5), bottomRight = (1,1))
编辑2:覆盖默认值

p1 = (3,5) 
p2 = (6,10)
r1 = Rectangle() 
print (r1.topLeft, r1.bottomRight)
> (0, 0) (1, 1)

r2 = Rectangle(p1, p2)
print (r2.topLeft, r2.bottomRight)
> (3, 5) (6, 10)

您可能只需要
新建点(0,0)
,而不仅仅是
(0,0)
。谢谢。如何为init方法编写呢?我尝试了def uuu init uuuu(self,new Point(0,0)topLeft,new Point(1,1)bottomRight):这是一个打字错误,我的错。当我以:r1=Rectangle()的形式运行它时,我得到了错误:TypeError:uuu init uuuuu()缺少两个必需的位置参数:“topLeft”和“bottomRight”。奇怪的是,我只是把它复制粘贴到一个新的笔记本上,它对我有用。你正在使用什么版本的python?是的,就是它!非常感谢你!我怎么能让它替换默认值0,0和1,1?我希望能够在新的地点通过考试。当我尝试它时,它发回了内存位置而不是点位置。