I l@ve RuBoard |
Subclassing Java ClassesYou can subclass Java classes like Python classes. Here's an example of subclassing the Java class java.util.Date (from MyDate.py):
Working with Java ConstructorsWhen you subclass a Java class, you call its superclass's constructor, just as you do in regular Python. As we saw, the MyDate.py example subclassed the Date class from java.lang and defined two constructors that called Date's base class constructors. As an exercise, look up the constructors of java.lang.Date in the Java API documentation. You'll find them in a section called Constructor Summary. The constructor of Date is defined in Java as
You call it from Python like this:
The second constructor of Date is defined in Java as
You call it from Python like this:
Here's how to call both Date constructors:
In Java, constructors are special language constructs that always have the same name as that of their class. In Python, the constructor is always a method called __init__. You call Java constructors in Python just as you do Python constructors; however, you can only do so in your subclass's constructor. Working with Java MethodsWhen subclassing a Java class, you can call all methods from a Java base class that you can from a Python base class. In our MyDate.py example, we subclass Date and add a __str__ method.
Unlike Python methods, Java methods can be overloaded, which essentially means that a Java class can have several methods with the same name. An example of this is java.io.OutputStream, which defines three write() methods:
As you can see, these methods can be called with different argument types. To subclass OutputStream, we need a way to override any or all of the write() methods. The following example shows how to do this. (Before we get started, however, read up on the OutputStream class in the Java API documentation.)
This code imports OutputStream, a Java class from the java.io package. OutputScreen subclasses OutputStream and overrides its write() methods (all three versions). Thus, each clause of the if statement in write() mimics a different overloaded signature of OutputStream's write()s.
To illustrate, the following code mimics the functionality of void write(int b) by determining that the first argument, b, is of type Integer.
This code mimics the functionality of void write(byte[]b) by determining that off wasn't passed (and that the first type was not Integer).
This mimics the functionality of void write (byte[]b, int off, int len) by a process of elimination.
Let's test each of these scenarios. Start the overload.py module from Jython with the �i option and try the following statements in an interactive session.
Essentially, we're calling all three write() methods. Here's the code step by step. Import the array() function from jarray to create Java byte arrays (byte[]).
Create an OutputScreen instance called ScreenOut.
Invoke the first form of the write() method, void write(int b).
Create a byte array for an argument for the second and third forms of write().
Invoke the second form of write(), void write(byte[] b).
Invoke the last form of write(), void write (byte[]b, int off, int len).
The output should be
|
I l@ve RuBoard |
No comments:
Post a Comment