How to inherit from MonkeyDevice?
I would like to extend the MonkeyDevice class of the monkeyrunner API. My derived class looks like this.
f开发者_如何学Gorom 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!
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)
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__
.
精彩评论