2015-04-24 88 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(),...