如何使用我在另一个类中使用的对象类的方法?

时间:2016-11-18 07:37:19

标签: list class python-3.x methods

基本上是这样的

class Key:

    def __init__(self, pressed):
        '''(Key, bool) -> NoneType
        '''
        self._pressed = pressed

    def change_press_state(self):
        self._pressed = not self._pressed

class KeyBoard:

    def __init__(self, how_many):
        '''(KeyBoard, int) -> NoneType
        Makes a Keyboard of 'how_many' keys.
        '''

        self._keys = []
        for i in range (0, how_many):
            self._keys.append(KeyBoard(False))

    def switch_status_of_key(self, index_of_key):
        '''(KeyBoard) -> NoneType
        Presses key if isn't and stops pressing key if it is.
        '''

如何在用户想要在switch_status_of_key中切换的特定密钥上调用change_press_state? 我在想像

self._keys[key] = self._keys[key].flip()

但显然不是因为我收到错误。

1 个答案:

答案 0 :(得分:0)

self._keys.append(KeyBoard(False))

应该是:

  self._keys.append(Key(False))

def switch_status_of_key(self, index_of_key):
        '''(KeyBoard) -> NoneType
        Presses key if isn't and stops pressing key if it is.
        '''
        self._keys[index_of_key].change_press_state()