如何从MonkeyDevice继承?MonkeyDevice

2023-09-08 09:38:21 作者:乱了年轻的心

我想延长MonkeyDevice类monkeyrunner的API。我的派生类看起来是这样的。

I would like to extend the MonkeyDevice class of the monkeyrunner API. My derived class looks like this.

from com.android.monkeyrunner import MonkeyDevice, MonkeyRunner

class TestDevice(MonkeyDevice):
    def __init__(self, serial=None):
        MonkeyDevice.__init__(self)
        self = MonkeyRunner.waitForConnection(deviceId=serial) 
        self.serial = serial

当我称之为 test_dev =为TestDevice(串行)从另一个模块,我得到了以下错误:

When I call test_dev = TestDevice(serial) from another module I get the following error:

    test_dev = TestDevice(serial)
TypeError: _new_impl(): 1st arg can't be coerced to com.android.monkeyrunner.core.IMonkeyDevice

我在做什么错了?

What am I doing wrong?

在此先感谢!

推荐答案

看样子你不能直接初始化 MonkeyDevice 实例,而不到一个工厂函数的调用为waitForConnection 。因此,你需要指定 __新__()功能,使 MonkeyDevice 识别实例从 IMonkeyDevice 继承你打电话之前它的 __的init __

It appears you cannot directly initialize a MonkeyDevice instance without a call to a factory function waitForConnection. So instead you need to assign self in your __new__() function so that MonkeyDevice recognizes the instance as inheriting from IMonkeyDevice before you call it's __init__

例如:

class TestDevice(MonkeyDevice):
    def __new__(self, serial=None):
        return MonkeyRunner.waitForConnection(deviceId=serial) 
    def __init__(self):
        MonkeyDevice.__init__(self)
相关推荐