[Jython] Generated Java file doesn't extend base class?

Ype Kingma ykingma at accessforall.nl
Tue Dec 17 14:16:20 EST 2002


Johannes Eble wrote:

> Hello all,
> 
> I try to create Java class files from Python for a Java Agents
> Environment (Grasshopper, see www.ikv.de). I use Jython for this task.
> 
> In order to create a minimal Grasshopper mobile agent you just have to
> extend de.ikv.grasshopper.agent.MobileAgent and to overwrite the
> live() methode. This looks as follows in Java:
> 
> // sample file HelloAgent.java
> import de.ikv.grasshopper.agent.MobileAgent;
> 
> public class HelloAgent extends MobileAgent {
> public void live() {
> log("Hello Agent!");
> }
> }
> 
> Therfore, I thought the equivalent Python code would look like this:
> 
> # HelloAgent.py
> from de.ikv.grasshopper.agent import MobileAgent
> 
> class HelloAgent(de.ikv.grasshopper.agent.MobileAgent):
> def live(self):
> "@sig public void live()"
>        log("Hello Agent!")
> 
> 
> 
> 
> I don't get a compilation error, but it doesn't work because the
> generated Java file (HelloAgent.java) extends java.lang.Object and not
> de.ikv.grasshopper.agent.MobileAgent
> 
> Why? Can't I force jythonc to generate a Java file that extends
> de.ikv.grasshopper.MobileAgent?

Try this:

from de.ikv.grasshopper.agent import MobileAgent

class HelloAgent(MobileAgent):
   def live(self):
       # Signature is not needed when
       # live() is defined in MobileAgent
       log("Hello Agent!")

I don't know why de.ikv.grasshopper.agent.MobileAgent
does not give an error message: I would expect a NameError.

Btw. you don't need jythonc just to inherit from a java class.
As an example, today I needed this:

from java.lang import Runnable

class RunnableFunction(Runnable):
    def __init__(self, f): self.f = f
    def run(self): self.f()

It allows to use a python function as a java.lang.Runnable,
and I used it as a helper class for another class in the 
same source file.

Have fun,
Ype

-- 
email at xs4all.nl



More information about the Python-list mailing list