如何继承MonkeyDevice?
我想扩展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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您无法在不调用工厂函数
waitForConnection
的情况下直接初始化MonkeyDevice
实例。因此,您需要在__new__()
函数中分配self
,以便MonkeyDevice
将该实例识别为从IMonkeyDevice
继承> 在调用它之前__init__
示例:
It appears you cannot directly initialize a
MonkeyDevice
instance without a call to a factory functionwaitForConnection
. So instead you need to assignself
in your__new__()
function so thatMonkeyDevice
recognizes the instance as inheriting fromIMonkeyDevice
before you call it's__init__
Example:
您似乎正在尝试扩展工厂调用
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 callwaitForConnection
.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 yourMonkeyDevice
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__
.