如何继承MonkeyDevice?

发布于 2024-12-05 07:30:39 字数 604 浏览 1 评论 0原文

我想扩展monkeyrunner API 的MonkeyDevice 类。 我的派生类看起来像这样。

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(serial) 时,出现以下错误:

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

我做错了什么?

提前致谢!

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

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?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-12-12 07:30:39

看来您无法在不调用工厂函数 waitForConnection 的情况下直接初始化 MonkeyDevice 实例。因此,您需要在 __new__() 函数中分配 self ,以便 MonkeyDevice 将该实例识别为从 IMonkeyDevice 继承> 在调用它之前 __init__

示例:

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

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__

Example:

class TestDevice(MonkeyDevice):
    def __new__(self, serial=None):
        return MonkeyRunner.waitForConnection(deviceId=serial) 
    def __init__(self):
        MonkeyDevice.__init__(self)
千纸鹤 2024-12-12 07:30:39

您似乎正在尝试扩展工厂调用 waitForConnection 返回的 MonkeyDevice 实例。

当您尝试在构造函数中替换 self 时,您会收到错误 (?)。
我怀疑您正在运行 Jython,因为 CPython 不会在这里抱怨,而是创建了一个局部变量 self 并且它的值丢失了。

无论如何,为了实现你想要的,你应该创建一个带有自定义 __new__ 而不是 __init__ 的类,从工厂获取你的 MonkeyDevice 实例并注入你的东西进入实例或其类/基础/等。

或者,您可以将 MonkeyDevice 包装到另一个类中,并通过 __getattr____setattr__ 传递类似猴子的调用和成员访问权限。

It seems you are trying to extend a MonkeyDevice instance returned by factory call waitForConnection.

When you try to substitute self inside the construtor you get an error (?).
I suspect you are running Jython, as CPython would not complain here, instead a local variable self is created and its value lost.

Anyway to achieve what you want you should create a class with custom __new__ rather than __init__, get your MonkeyDevice instance from the factory and inject your stuff into the instance or it's class/bases/etc.

Alternatively you could wrap MonkeyDevice into another class and pass monkey-ish calls and member access though __getattr__ and __setattr__.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文