2017-07-31 59 views
0

我想讓兩個實例具有相同名稱一樣,Python的OOP,不能覆蓋實例

a = SomeClass(someAttr1) 
a = SomeClass(someAttr2) 

,使新人們應該覆蓋前一個。

我也試過這樣:

a = SomeClass(someAttr1) 
a = None 
a = SomeClass(someAttr2) 

我試過,但它不會覆蓋以前的實例,並增加了它本身,有沒有辦法做到這一點?

下面是代碼:

### Do not change the Location or Campus classes. ### 
### Location class is the same as in lecture.  ### 
class Location(object): 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def move(self, deltaX, deltaY): 
     return Location(self.x + deltaX, self.y + deltaY) 

    def getX(self): 
     return self.x 

    def getY(self): 
     return self.y 

    def dist_from(self, other): 
     xDist = self.x - other.x 
     yDist = self.y - other.y 
     return (xDist ** 2 + yDist ** 2) ** 0.5 

    def __eq__(self, other): 
     return (self.x == other.x and self.y == other.y) 

    def __str__(self): 
     return '<' + str(self.x) + ',' + str(self.y) + '>' 


class Campus(object): 
    def __init__(self, center_loc): 
     self.center_loc = center_loc 

    def __str__(self): 
     return str(self.center_loc) 


class MITCampus(Campus): 
    """ A MITCampus is a Campus that contains tents """ 
    tents_list = [] 
    def __init__(self, center_loc, tent_loc=Location(0, 0)): 
     """ Assumes center_loc and tent_loc are Location objects 
     Initializes a new Campus centered at location center_loc 
     with a tent at location tent_loc """ 
     # Your code here 
     Campus.__init__(self, center_loc) 
     self.tent_loc = tent_loc 
     self.tents_list.append(self.tent_loc) 

    def add_tent(self, new_tent_loc): 
     """ Assumes new_tent_loc is a Location 
     Adds new_tent_loc to the campus only if the tent is at least 0.5 distance 
     away from all other tents already there. Campus is unchanged otherwise. 
     Returns True if it could add the tent, False otherwise. """ 
     # Your code here 

     new_tent_flag = True 
     for loc in self.tents_list: 
      if loc == new_tent_loc or new_tent_loc.dist_from(loc) < 0.5: 
       new_tent_flag = False 

     if new_tent_flag: 
      self.tents_list.append(new_tent_loc) 
      return True 
     else: 
      return False 


    def get_tents(self): 
     """ Returns a list of all tents on the campus. The list should contain 
     the string representation of the Location of a tent. The list should 
     be sorted by the x coordinate of the location. """ 
     # Your code here 
     new_list_sorted = sorted(self.tents_list, key=lambda tent: tent.getX()) 
     str_list = [] 
     for x in new_list_sorted: 
      str_list.append(x.__str__()) 

     return str_list 

測試用例:

Test: 0 


     c = MITCampus(Location(1,2)) 
     print(c.add_tent(Location(1,2))) 
     print(c.add_tent(Location(0,0))) 
     print(c.add_tent(Location(2,3))) 
     print(c.add_tent(Location(2,3))) 
     print(c.get_tents()) 


Output: 

    True 
    False 
    True 
    False 
    ['<0,0>', '<1,2>', '<2,3>'] 

Test: 1 


     init campus with default tent loc 
     c = MITCampus(Location(-1,-2)) 
     print(sorted(c.get_tents())) 


Output: 

    ['<0,0>','<0,0>', '<1,2>', '<2,3>'] 

Expected Output: 

     ['<0,0>'] 

可以看出的是,二審應覆蓋前一個,而是它被添加。代碼中的問題是什麼?以及如何解決它?

+2

請創建一個[mcve]。 「不覆蓋」是非常模糊的。你爲什麼認爲舊的對象沒有被覆蓋? –

+0

我不知道爲什麼你刪除了這些調用,但現在這個問題沒有任何意義。你應該創建一個代碼的最小例子,而不是刪除整個事情。 – spicypumpkin

+0

我刪除了代碼,因爲它是任務,我不知道我被允許將它放在這裏,也破壞了任務的目的。 但是是的,我會更好地更新它 – fluffy

回答

0

好的, 您的tents_list屬性是類屬性,因此即使您的c對象被覆蓋,tents_list屬性保持不變。 最好將你的tents_list作爲對象參數,這樣tents_list屬性也會被覆蓋。

def __init__(self, center_loc, tent_loc=Location(0, 0)): 
    Campus.__init__(self, center_loc) 
    self.tents_list = [] # <--- Add this 
    self.tent_loc = tent_loc 
    self.tents_list.append(self.tent_loc) 
+0

你應該更新你以前的答案,而不是生成另一個 –

+0

好吧,感謝您的建議。 – Zcode

+0

這工作得很好,謝謝,但只有一個錯誤,我應該更新關於該職位? – fluffy

0

對象被覆蓋,但你定義tents_list爲類變量,因此所有的MITCampus份額此列表中的實例。 因此,任何新的實例都會添加到該列表中,並認爲您沒有被「覆蓋」。

如果您想要「覆蓋」行爲,請將tents_list轉換爲__init__方法,如self.tents_list。只有這樣它纔會對每個實例都是唯一的。