如何从MonkeyDevice继承? [英] How to inherit from MonkeyDevice?

查看:123
本文介绍了如何从MonkeyDevice继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想延长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)

这篇关于如何从MonkeyDevice继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆