回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我的拳击比我的体重还重,但请容忍这个业余的Python。我是一个专业的PHP开发人员,以前几乎没有接触过这种语言。</p>
<p>我想做的是在类中调用一个方法…听起来很简单吗?对于“self”指的是什么,以及在类内和类外调用这种方法的正确过程,我感到非常困惑。</p>
<p>有人能给我解释一下如何用变量<code>RIGHT</code>调用<code>move</code>方法吗。我试过在几个“学习python”网站上研究这个问题,并在StackOverflow上搜索,但没有成功。任何帮助都将不胜感激。</p>
<p>下面的类在Scott的Python脚本中工作,该脚本由终端GUI(urwid)访问。</p>
<p>我使用的函数是Scott Weston的导弹发射器Python脚本,我正试图将其连接到PHP web服务器。</p>
<pre><code>class MissileDevice:
INITA = (85, 83, 66, 67, 0, 0, 4, 0)
INITB = (85, 83, 66, 67, 0, 64, 2, 0)
CMDFILL = ( 8, 8,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)
STOP = ( 0, 0, 0, 0, 0, 0)
LEFT = ( 0, 1, 0, 0, 0, 0)
RIGHT = ( 0, 0, 1, 0, 0, 0)
UP = ( 0, 0, 0, 1, 0, 0)
DOWN = ( 0, 0, 0, 0, 1, 0)
LEFTUP = ( 0, 1, 0, 1, 0, 0)
RIGHTUP = ( 0, 0, 1, 1, 0, 0)
LEFTDOWN = ( 0, 1, 0, 0, 1, 0)
RIGHTDOWN = ( 0, 0, 1, 0, 1, 0)
FIRE = ( 0, 0, 0, 0, 0, 1)
def __init__(self, battery):
try:
self.dev=UsbDevice(0x1130, 0x0202, battery)
self.dev.open()
self.dev.handle.reset()
except NoMissilesError, e:
raise NoMissilesError()
def move(self, direction):
self.dev.handle.controlMsg(0x21, 0x09, self.INITA, 0x02, 0x01)
self.dev.handle.controlMsg(0x21, 0x09, self.INITB, 0x02, 0x01)
self.dev.handle.controlMsg(0x21, 0x09, direction+self.CMDFILL, 0x02, 0x01)
</code></pre>