How to extend jython class in java class

I would like to extend the jython class in java class

public class JavaClass extends JythonClass 

How to import a Jython class? Do I need to compile it? A link to the documentation will already be useful.

Example:

 class JythonClass(threading.Thread): def do(self): print("hallo") 

-

 public class JavaClass extends JythonClass { public void hello() { System.out.print("hallo")} } 
+7
java jython
source share
2 answers

You can extend the Jython class in Java so that the result can be used in Jython by creating a Jython class that extends both the "Java subclass" and the Jython "superclass". Let's say you have this Jython class:

 class JythonClass(object): def get_message(self): return 'hello' def print_message(self): print self.get_message() 

You can create your Java class, in part, without extending anything:

 public class JavaClass { private String message; public String get_message() { return message; } protected JavaClass(String message) { this.message = message; } } 

And then call the “expanding” relationship in Jython:

 from javapackage import JavaClass as _JavaClass from jythonpackage import JythonClass class JavaClass(_JavaClass, JythonClass): pass 

Now this will work:

 obj = JavaClass('goodbye') obj.print_message() 

If instead you want to extend the Jython class in Java and use it as a regular Java class, the easiest way would be to use composition instead of inheritance:

  • create a Java interface for methods in your Jython class
  • so that your Jython class extends the Java interface (either by modifying the code of the Jython class, or in some way, as described above).
  • create your Java subclass as an implementing interface, with an instance of the Jython "superclass" as a private field
  • any method in your Java subclass that you don't want to override, just call that method in a private Jython instance
+9
source share

I think this is covered by Ch10 from the final Jython tutorial

In this example, the author uses the BuildingType interface, which can be changed to an abstract class

 public abstract class BuildingType { public abstract String getBuildingName(); public abstract String getBuildingAddress(); public abstract String getBuildingId(); @Override public String toString(){ return "Abstract Building Info: " + getBuildingId() + " " + getBuildingName() + " " + getBuildingAddress(); } } 

which you can expand with your own methods, in this case I added toString() and used it in 'print' instead of the author’s source code:

 public class Main { private static void print(BuildingType building) { System.out.println(building.toString()); } public static void main(String[] args) { BuildingFactory factory = new BuildingFactory(); BuildingType building1 = factory.create("BUILDING-A", "100 WEST MAIN", "1") print(building1); BuildingType building2 = factory.create("BUILDING-B", "110 WEST MAIN", "2"); print(building2); BuildingType building3 = factory.create("BUILDING-C", "120 WEST MAIN", "3"); print(building3); } } 

You can duplicate this code using the code from CH10 in the Definition Guide for Jython, and all credits should be sent to the author - I just changed Interface to the Abstract class.

You will also consider using the default Java 7 method in the interface.

+2
source share

All Articles