Subclassing......

#VEDALA NARASIMHA KUMAR# PA0215481 at ntu.edu.sg
Wed May 24 12:07:38 EDT 2000


 
 

Hi,

here's what you gotto do...it worked

--[ Vehicle.java ]-----------------------------

// Java class that will be subclassed by Car.py
public class Vehicle {

  public void printLicenseplate() {
      System.out.println("ABC123");
  }

}

--[ Car.py ]-----------------------------------
# JPython class that will subclass Java Vehicle
import java  
import Vehicle
class Car(Vehicle):
    def start(self):    #dun forget to include self

        self.on=1

    def stop(self):
        self.on=0

    def printStatus(self):
        if not self.on:
            print "The car is stopped"
        else:
            print "The car is running"

compile it with "jpythonc --core -A org.python.core Car.py"
and copy the generated class file to workin directory.

Here's the trick for the final one

--[ VehicleTest.java ]------------------------
// A Java test class just to test the two classes above
import java.lang.*;
import Car;
import org.python.core.*;
import org.python.util.PythonInterpreter;
public class VehicleTest extends Car{

   public static void main(String[] args) throws PyException {
       PythonInterpreter py=new PythonInterpreter()
      System.out.println("Starting");
       py.set("c", new Car());
      //Car c = new Car();

       // None of these calls to methods in the
       // Car object will be accepted.
       py.exec("c.printLicenseplate()");
       py.exec("c.start()");
       py.exec("c.printStatus()");
   }

}

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

I have searched through lots of places for information
about how this should be done without finding a good
answer. Since the output from jpythonc must be useful
somehow, there must be an obvious answer, but I fail to
see it right now.

Any feedback is greatly appreciated.


Regards,
Henrik W.




More information about the Python-list mailing list