android – 如何从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

我究竟做错了什么?

提前致谢!

解决方法

看来你不能在没有调用工厂函数waitForConnection的情况下直接初始化MonkeyDevice实例.因此,您需要在__new __()函数中指定self,以便MonkeyDevice在将您的实例称为__init__之前将该实例识别为继承自IMonkeyDevice

例:

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...