簡體   English   中英

塞特犬和蓋特斯蟒蛇

[英]Setters and Getters python

我環顧四周,但我嘗試的一切似乎都沒有得到任何結果。 我只需要為一個將數字轉換為字符串而反之亦然的類設置自定義setter和getter。 因為python不支持我使用字典。

class Ship(object):
    def __init__(self, type):
        self.fixed = define_direction()

        self.row = random_row(board, self)
        self.col = random_col(board, self)
        self.type = self.set_type(type)
        print "Type:", str(self.type)

    def set_type(self, type):
        return {
            0 : "Patrol boat",#2
            1 : "Destroyer",#3
            2 : "Submarine",#3
            3 : "Battleship",#4
            4 : "Aircraft carrier",#5
            }.get(type, "Patrol boat") 
    def get_type(self):
        return {
            "Patrol boat" : 0,#2
            "Destroyer" : 1,#3
            "Submarine" : 2,#3
            "Battleship" : 3,#4
            "Aircraft carrier" : 4,#5
              }.get(self.type, 0) 
    def __repr__(self):
        return "Ship. Type: ", self.get_type()

不確定self.type = self.set_type(type)是否合法,但它似乎是從類內部調用函數的唯一方法。

__init__(self, type) - >“type”作為數字傳遞,它應該被轉換並存儲為字符串,而不是在調用getter時重新轉換為數字。 (也許有更好的方法 - 使用外部字典進行轉換並只存儲數字..?

  • PS1:調用外部函數random_row(board, self)random_col傳遞self是否合法,即使它沒有正確初始化,還是應該將類中的函數作為另一個setter移動?

  • PS2:調用__repl__

     def __repr__(self): return "Ship. Type: ", str(self.get_type()), str(self.type()) 

    收益:

     Traceback (most recent call last): File "/Users/xx/Documents/python/battleship/battleship.py", line 85, in <module> print ship.__repr__() File "/Users/xx/Documents/python/battleship/battleship.py", line 74, in __repr__ return "Ship. Type: ", str(self.get_type()), str(self.type()) TypeError: 'str' object is not callable 

    僅在get_type()上調用它

     def __repr__(self): return "Ship. Type: ", str(self.get_type()) 

    收益:

     Type: Submarine ('Ship. Type: ', '2') 

希望它足夠清楚。

您可以使用@property裝飾器來管理您的type屬性:

class Ship(object):
    def __init__(self, type):
        self.fixed = define_direction()

        self.row = random_row(board, self)
        self.col = random_col(board, self)
        self.type = type
        print "Type:", str(self.type)

    @property
    def type(self):
        return {
            "Patrol boat": 0,
            "Destroyer": 1,
            "Submarine": 2,
            "Battleship": 3,
            "Aircraft carrier": 4,
        }.get(self._type, 0) 

    @type.setter
    def type(self, type):
        self._type = {
            0: "Patrol boat",
            1: "Destroyer",
            2: "Submarine",
            3: "Battleship",
            4: "Aircraft carrier",
        }.get(type, "Patrol boat") 

    def __repr__(self):
        return "Ship. Type: " + self._type

你的__repr__應該總是返回一個字符串; 你回來了一個元組。 您的錯誤是由您的self.type()調用引起的; 因為self.type在您的代碼中存儲一個字符串,所以您試圖將該字符串視為可調用的。

可以從__init__調用其他函數(在類之外); 它只是你的實例上的另一種方法,只考慮具有和尚未設置的屬性。 但是,如果函數依賴於self信息並且在類外沒有用處,我會將它移動到帶有_前綴的類中,以指示它是類實現的內部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM