Creating classes in the Jython User Library

Post any questions, ideas, or topics related to Jython and Python scripting.
Post Reply
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Creating classes in the Jython User Library

Post by tomw »

If you want to create some "classes" (in the Object Oriented Programming sense) in your Jython User Library, here's a template to get you started:

Code: Select all

class What:
   def __init__(self, v):
     self.x = v;
   def get(self):
     return self.x
   def set(self, v):
     self.x = v;

[Note that the "self" as the first parameter of each method is required, and that the
__init__(self, v):
is the constructor for this class.]

"Save" this and then in the Jython shell, you can do the following to check it out. Note that "z" and "m" are different instances of the class "What"....the possibilities are endless....

z=What(13)
print z.get()
13
z.set(99)
print z.get()
99
m = What(15)
print m.get()
15
print z.get()
99
Post Reply