2015-04-24 114 views
0

好吧,所以我必须创建两个类(两个不同的脚本),都称为Block,它存储有关矩形块的位置和大小的信息。版本1应具有用于存储块中心坐标的属性(或者作为单独的x坐标或y坐标,或者作为一对数字)以及块的宽度和高度。版本2应该具有用于​​存储左下角(“SW”角)的坐标和右上角(“NE”角)的坐标的属性。一组函数的可选参数 - python

所以我知道如何为每个构造函数单独设置构造函数,但对于这个赋值,两个版本都应该有一个构造函数,该构造函数将中心坐标与宽度和高度一起作为浮点数字)或表示块的任何两个对角的两对坐标。这是我迄今为止的尝试:

class Block: 
    """Stores information about the position and size of a rectangular block. 

    Attributes: x-coordinate (int), y-coordinate (int), width (int), height (int) OR northeast corner (int) and southwest corner (int)""" 

    def __init__(self, center = '', width = '', height = '', SW = '', NE = ''): 
     """A constructor that assigns attributes to the proper variables 

     Block, tuple, tuple -> None""" 
     self.center = center 
     self.width = width 
     self.height = height 
     self.SW = SW 
     self.NE = NE 

但我敢肯定,实际上并不按照我想要的方式工作。基本上我需要能够输入一组变量作为中心,宽度和高度,或者我需要输入两个角落。有没有办法做到这一点?

+0

'B =块(SW = 3,宽度= 1)'... ..运作良好....这是问题所在? –

+0

你想如何使用构造函数?请举一些例子。你是否希望所有的字段都相应地设置或者保持为无? –

回答

2

您必须检查传递给该函数的哪些参数并相应地采取行动。通常,您要做的是选择一个规范表示形式来实际存储数据,并且将任何传递给它的参数转换为规范形式。例如:

# Use None to represent missing data. Think about it: "hello" is not a 
# valid width; neither is "". 
def __init__(self, center=None, width=None, height=None, SW=None, NE=None): 
    """A constructor that assigns attributes to the proper variables 

    Block, tuple, tuple -> None""" 

    if center is not None and width is not None and height is not None: 
     # If either SW or NE is given, ignore them 
     self.center = center 
     self.width = width 
     self.height = height 
    elif SW is not None and NE is not None: 
     # _convert_corners is a helper function you define 
     self.center, self.width, self.height = _convert_corners(SW, NE) 
    else: 
     # Not entirely true. Give width, height, and one corner, you 
     # could reconstruct the center, but this is just an example. 
     raise ValueError("Insufficient information to construct Block") 

您可以使用属性来计算在飞行中的其他属性,而不是将它们存储冗余:

@property 
def SW(self): 
    # return the south-west corner as computed from 
    # self.center, self.height, and self.width 

@property 
def NE(self): 
    # return the north-east corners computed from 
    # self.center, self.height, self.width 

另一种方法是使用类方法来提供备用构造。

def __init__(self, center, width, height): 
    "Define a block by its center, width, and height" 
    self.center = center 
    self.width = width 
    self.height = height 

@classmethod 
def from_corners(self, sw, ne): 
    "Define a block by two corners" 
    c, w, h = _convert_corners(sw, ne) 
    return Block(c, w, h) 

在使用中:

# For demonstration purposes, I'm assuming points like the center 
# and the corners are simple tuples of integer coordinates 
b1 = Block((10, 50), 5, 7) 
b2 = Block.from_corners((20, 30), (40, 70)) 
0

你几乎没有。尝试这样的事情......

class Block: 
    def __init__(self, center = '', width = '', height = '', SW = '', NE = ''): 
     if SW != '' or NE != '': 
      if SW == '' and NE == '': # usage error 
       return None    # throw an exception here 
      self.center = getCenterFromCorners(SW, NE) # ((sw[0]+ne[0])/2, ...) 
      self.width = getWidthFromCorners(SW, NE) # abs(sw[0]-ne[0]) 
      self.height = getHeightFromCorners(SW, NE) # abs(sw[1]-ne[1]) 
     else: 
      if center == '' or width == '' or '' height == '': 
       return None    # throw exception 
      self.center = center 
      self.width = width 
      self.height = height 
     return self 

# usage: block1 and block2 should be similar 
block1 = Block(center=(10,20), height=2, width=4) 
block2 = Block(SW=(9,18), NE=(11,22)) 

我相信你可以替换代码getCenterFromCorners(),...