Jython继承自Java类 [英] Jython inherit from Java class

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

问题描述

我编写了一个继承自Java类的Jython类,并且(即

计划)覆盖了一个方法。一切都应该保持不变。


如果我运行它没有任何反应,而如果我运行Java类它说:

用法:java fit.FitServer [ - v]主机端口socketTicket

-v verbose


我认为这是因为我不理解

继承的jython机制(还)。


JyFitServer.py:

===========

import fit。 FitServer

import fitnesse.components.FitProtocol

来自fit.Parse导入Parse

来自fit.Fixture导入夹具


#继承自Robert C.的原始Java FitServer实现。

Martin和Micah D. Martin

class FitServer(fit.FitServer):

#超类的调用构造函数

def __init __(self,host,port,verbose):

FitServer .__ init __(self,host,port,verbose)


#覆盖进程方法

def进程(个体经营):

self.fixture.listener = self.fixtureListener

print" hello,我是JyFitServer!"

试试:

size = FitProtocol.readSize(self.socketReader)

如果尺寸> 0:

试试:

document =

FitProtocol.readDocument(self.socketReader,size)

tables =解析(文件)

fixture = Fixture()

fixture.listener = self.fixtureListener;

fixture.doTables(tables)

self.counts.tally(self.fixture.counts)

除了FitParseException,e:

self.exception(e)

除了Exception,e:

self.exception(e)


请帮助,

Mark Fink

解决方案

[Mark Fink]

我写了一个继承自Java类的Jython类(那就是
计划)覆盖一种方法。一切都应该保持不变。

如果我运行这没有任何反应,而如果我运行Java类它说:
用法:java fit.FitServer [-v]主机端口socketTicket
-v verbose

我认为这是因为我不了解
继承的jython机制(还)。


1.你在运行jythonc吗?


如果是,我认为你的班级和档案应该有相同的名称,即

Class FitServer应位于名为FitServer.py的文件中。我建议用你的类调用你的类与基类不同的东西,例如

MyJythonFitServer,以防止命名空间冲突。


2。如果你的主要功能在jython中?如果是,请发布代码,以便我们

可以看到你如何实例化你的对象?


3.你是如何运行它的?即向我们展示一个命令行会话,

使用你的班级。

JyFitServer.py:
===========
class FitServer(fit.FitServer):
#call构造函数的超类
def __init __(self,host,port,verbose):
FitServer .__ init __(self,host,port,verbose)
^^^^^^^^^

不应该这样:

fit.FitServer .__ init __(self,host,port,verbose)




我不确定后者是你的问题的原因,但可能是。


HTH,


-

alan kennedy

-------------------- ----------------------------------

email alan: http://xhaus.com/contact/alan


Mark Fink写道:

我编写了一个继承自Java类的Jython类(这就是
计划)会覆盖一个方法。一切都应该保持不变。

如果我运行这没有任何反应,而如果我运行Java类它说:
用法:java fit.FitServer [-v]主机端口socketTicket
-v verbose


听起来Java类有一个main()函数。当你将它作为一个应用程序运行时,Java运行时会在你告诉它运行的

类中查找main()函数。

即使在Java中,继承也不会保留这种行为,子类

必须有自己的main()才能执行。


Jython(和Python)的工作方式略有不同,当你运行它时,你的模块会从头到尾执行
。任何不在
类或函数定义中的语句都将被执行。 (实际上,类和

函数定义被执行,但不是以相同的方式执行...)


如果你有可能得到你想要的行为把行

导入sys

fit.FitServer.main(sys.argv)


在文件的末尾。这将运行Java

类的main()函数。


Kent

我认为这是因为我没有了解
继承的jython机制(还)。

JyFitServer.py:
===========
import fit.FitServer <来自fit.Farse导入Parse
来自fit.Fixture导入夹具

#继承自Robert C.的原始Java FitServer实现。
从fit.Parse导入fitnesse.components.FitProtocol
Martin和Micah D. Martin
类FitServer(fit.FitServer):
#call构造函数的超类
def __init __(self,host,port,verbose):
FitServer .__ init __(self,host,port,verbose)

#ifover进程方法
def进程(self):
self.fixture.listener = self.fixtureListener
打印你好,我是JyFitServer!尝试:
size = FitProtocol.readSize(self.socketReader)
如果尺寸> 0:
尝试:
document =
FitProtocol.readDocument(self.socketReader,size)
tables = Parse(document)
fixture = Fixture()
fixture.listener = self.fixtureListener;
fixture.doTables(tables)
self.counts.tally(self.fixture.counts)
除了FitParseException,e:
self。例外(e)
除了例外,e:
self.exception(e)

请帮助,
Mark Fink



艾伦,肯特,非常感谢这真的有帮助!

但我认为继承仍有问题。我使用原始随附的java

testsuit来测试服务器。如果我使用

Java FitServer,则可以完成测试套件。我在课堂上评论了一切

并且它不起作用?

当我运行JUnit测试服时,这是跟踪:

java。 io.IOException:CreateProcess:jython

D:\ AUT_TEST \workspace \JyFIT \fit\JyFitServer.py

-Dpython.path =''D :\ AUT_TEST \ workspace \JyFIT''

D:\ AUT_TEST \\\ tidnesse \ fitnesse.jar localhost 1234 23错误= 2

在java。 lang.ProcessImpl.create(本机方法)

at java.lang.ProcessImpl。< init>(未知来源)

at java.lang.ProcessImpl.start(Unknown来源)

在java.lang.ProcessBuilder.start(未知来源)

在java.lang.Runtime.exec(未知来源)

at java.lang.Runtime.exec(Unknown Source)

at java.lang.Runtime.exec(Unknown Source)

at

FitServerTest .FitServerTest.prepareSessionProcess(FitServerTest.java:163)

at

FitServerTest.FitServerTest.testSimpl eStartUp(FitS erverTest.java:36)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source )

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)
$ b $ jun at junit.framework.TestCase.runTest(TestCase.java:154)

在junit.framework.TestCase.runBare(TestCase.java:127)

在junit .framework.TestResult

I wrote a Jython class that inherits from a Java class and (thats the
plan) overrides one method. Everything should stay the same.

If I run this nothing happens whereas if I run the Java class it says:
usage: java fit.FitServer [-v] host port socketTicket
-v verbose

I think this is because I do not understand the jython mechanism for
inheritance (yet).

JyFitServer.py:
===========
import fit.FitServer
import fitnesse.components.FitProtocol
from fit.Parse import Parse
from fit.Fixture import Fixture

# Inherit from original Java FitServer Implementation by Robert C.
Martin and Micah D. Martin
class FitServer(fit.FitServer):
# call constructor of superclass
def __init__(self, host, port, verbose):
FitServer.__init__(self, host, port, verbose)

# override process method
def process(self):
self.fixture.listener = self.fixtureListener
print "hello, I am JyFitServer!"
try:
size = FitProtocol.readSize(self.socketReader)
if size > 0:
try:
document =
FitProtocol.readDocument(self.socketReader, size)
tables = Parse(document)
fixture = Fixture()
fixture.listener = self.fixtureListener;
fixture.doTables(tables)
self.counts.tally(self.fixture.counts)
except FitParseException, e:
self.exception(e)
except Exception, e:
self.exception(e)

Please help,
Mark Fink

解决方案

[Mark Fink]

I wrote a Jython class that inherits from a Java class and (thats the
plan) overrides one method. Everything should stay the same.

If I run this nothing happens whereas if I run the Java class it says:
usage: java fit.FitServer [-v] host port socketTicket
-v verbose

I think this is because I do not understand the jython mechanism for
inheritance (yet).
1. Are you running jythonc?

If yes, I think your class and file should have the same name, i.e.
Class FitServer should be in a file called "FitServer.py". I recommend
calling your class something different from the base class, e.g.
MyJythonFitServer, to prevent namespace clashes.

2. If your main function in jython? If yes, please post the code so we
can see how you''re instantiating your objects?

3. How are you running this? I.e. show us a command line session which
uses your class.
JyFitServer.py:
=========== class FitServer(fit.FitServer):
# call constructor of superclass
def __init__(self, host, port, verbose):
FitServer.__init__(self, host, port, verbose) ^^^^^^^^^
Shouldn''t this be:
fit.FitServer.__init__(self, host, port, verbose)



I''m not sure the latter is cause of your problems, but it might be.

HTH,

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan


Mark Fink wrote:

I wrote a Jython class that inherits from a Java class and (thats the
plan) overrides one method. Everything should stay the same.

If I run this nothing happens whereas if I run the Java class it says:
usage: java fit.FitServer [-v] host port socketTicket
-v verbose
It sounds like the Java class has a main() function. When you run it as
an application, the Java runtime looks for the main() function in the
class you tell it to run.

Even in Java, this behaviour is not preserved by inheritance, a subclass
has to have its own main() to be executable.

Jython (and Python) works a little differently, your module is executed
from start to finish when you run it. Any statements that are not inside
a class or function definition will be executed. (Actually the class and
function definitions are executed to, but not in the same way...)

You might get the behaviour you want if you put the line
import sys
fit.FitServer.main(sys.argv)

at the end of your file. This will run the main() function of the Java
class.

Kent

I think this is because I do not understand the jython mechanism for
inheritance (yet).

JyFitServer.py:
===========
import fit.FitServer
import fitnesse.components.FitProtocol
from fit.Parse import Parse
from fit.Fixture import Fixture

# Inherit from original Java FitServer Implementation by Robert C.
Martin and Micah D. Martin
class FitServer(fit.FitServer):
# call constructor of superclass
def __init__(self, host, port, verbose):
FitServer.__init__(self, host, port, verbose)

# override process method
def process(self):
self.fixture.listener = self.fixtureListener
print "hello, I am JyFitServer!"
try:
size = FitProtocol.readSize(self.socketReader)
if size > 0:
try:
document =
FitProtocol.readDocument(self.socketReader, size)
tables = Parse(document)
fixture = Fixture()
fixture.listener = self.fixtureListener;
fixture.doTables(tables)
self.counts.tally(self.fixture.counts)
except FitParseException, e:
self.exception(e)
except Exception, e:
self.exception(e)

Please help,
Mark Fink



Alan, Kent, many thanks this really helped!
But there is still a problem I guess with inheritance. I use the java
testsuit supplied with the original to test the server. If I use the
Java FitServer the testsuite can be completed. I commented everything
out from my class and it does not work??
Thats the trace when I run the JUnit testsuit:
java.io.IOException: CreateProcess: jython
D:\AUT_TEST\workspace\JyFIT\fit\JyFitServer.py
-Dpython.path=''D:\AUT_TEST\workspace\JyFIT''
D:\AUT_TEST\fitnesse\fitnesse.jar localhost 1234 23 error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at
FitServerTest.FitServerTest.prepareSessionProcess( FitServerTest.java:163)
at
FitServerTest.FitServerTest.testSimpleStartUp(FitS erverTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154 )
at junit.framework.TestCase.runBare(TestCase.java:127 )
at junit.framework.TestResult


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

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