【问题标题】:How do I use a method of a class who's object I'm using in another class?如何使用我在另一个类中使用的对象的类的方法?
【发布时间】:2016-11-18 07:37:18
【问题描述】:

基本上是这样的

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()

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

【问题讨论】:

    标签: list class python-3.x methods


    【解决方案1】:
    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()
    

    【讨论】:

    • 对了!没有平等,感谢我刚刚对这些错误进行了任意快速的捕获。
    猜你喜欢
    • 2020-12-31
    • 2013-04-21
    • 1970-01-01
    • 2019-06-25
    • 2014-12-30
    • 2013-02-17
    • 2011-06-17
    • 1970-01-01
    • 2019-03-11
    相关资源
    最近更新 更多