class State():
def __init__(self, state):
self._state = state
def change(self): #should not return anything, just changes the state
self._state = not self._state
class Coin():
def __init__(self, coin):
self._coin = []
for i in range(coin):
self._coin.append(State(False)._state)
def __str__(self):
return "coins: " + str(self._coin)
def change_state(self, n):
self._coin[n] = State(self._coin[n]).change()
我遇到的問題是change_state方法將狀態從False/True更改爲None,而不是將其更改爲True或False。我不確定我做錯了什麼。更改布隆的狀態
c1 = Coin(10)
print(c1)
coins: [False, False, False, False, False, False, False, False, False, False]
c1.change_state(4)
print(c1)
coins: [False, False, False, False, None, False, False, False, False, False]
你實際上並沒有在這裏使用'State';只是在'_coin'中使用布爾值,並簡單地使'change_state'變成'self._coin [n] = not self._coin [n]' – jonrsharpe
爲什麼你有一個單獨的'State'類? – deltab