Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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中用python定义类_Python_Class_Self - Fatal编程技术网

在python中用python定义类

在python中用python定义类,python,class,self,Python,Class,Self,所以我试图为一个矩形创建一个类,我试图找到矩形的右下角。通过在左上角添加高度和宽度,可以找到矩形的右下角。例如(2,3),4,7将使底角为(6,10)。然而,我不相信我的代码是正确的。这是我第一次使用类,所以一些关于如何解释的提示和技巧将非常有用 class Rectangle(object): def __init__(self, (top left corner), width, height): """ __init__(self, (x, y), integer, in

所以我试图为一个矩形创建一个类,我试图找到矩形的右下角。通过在左上角添加高度和宽度,可以找到矩形的右下角。例如(2,3),4,7将使底角为(6,10)。然而,我不相信我的代码是正确的。这是我第一次使用类,所以一些关于如何解释的提示和技巧将非常有用

class Rectangle(object):

def __init__(self, (top left corner), width, height):
    """
    __init__(self, (x, y), integer, integer)
    """

    self._x = x
    self._y = y
    self._width = width
    self._height = height

def get_bottom_right(self):
    x + self.width = d
    y + self.height = t

return '' + d,t
代码在这里运行:


代码在这里运行:

我想你想要的是这个

class Rectangle(object):
  def __init__(self, pos, width, height):
    self._x = pos[0]
    self._y = pos[1]
    self._width = width
    self._height = height
  def get_bottom_right(self):
    d = self._x + self._width
    t = self._y + self._height
    return d,t
你可以这样用

class Rectangle(object):
  def __init__(self, top_corner, width, height):
    self._x = top_corner[0]
    self._y = top_corner[1]
    self._width = width
    self._height = height

  def get_bottom_right(self):
    d = self._x + self.width
    t = self._y + self.height
    return (d,t)
另外,你也可以通过做一个
类来节省一些时间

# Makes a rectangle at (2, 4) with width
# 6 and height 10
rect = new Rectangle((2, 4), 6, 10) 

# Returns (8, 14)
bottom_right = rect.get_bottom_right

我想你想要的是这个

class Rectangle(object):
  def __init__(self, pos, width, height):
    self._x = pos[0]
    self._y = pos[1]
    self._width = width
    self._height = height
  def get_bottom_right(self):
    d = self._x + self._width
    t = self._y + self._height
    return d,t
你可以这样用

class Rectangle(object):
  def __init__(self, top_corner, width, height):
    self._x = top_corner[0]
    self._y = top_corner[1]
    self._width = width
    self._height = height

  def get_bottom_right(self):
    d = self._x + self.width
    t = self._y + self.height
    return (d,t)
另外,你也可以通过做一个
类来节省一些时间

# Makes a rectangle at (2, 4) with width
# 6 and height 10
rect = new Rectangle((2, 4), 6, 10) 

# Returns (8, 14)
bottom_right = rect.get_bottom_right

我想找到矩形的底部。基本上,我想添加topleftcorner(将是x,y),并将宽度添加到x,高度添加到y。谢谢你的回复,虽然我想找到矩形的底部。基本上,我想添加topleftcorner(将是x,y),并将宽度添加到x,高度添加到y。谢谢你的回复。谢谢,这就是我想要的。我不知道如何用x和y来定义上角。谢谢,这就是我想要的。我不知道如何用x和y定义top_corner。当你发布代码时,你可以确保它的格式正确,因为它在python中很重要。对不起,我下次发布代码时会记住这一点。当你发布代码时,你可以确保它的格式正确,因为它在python中很重要。对不起,我下次发布时会记住这一点。