class definition in WLST Script |
In WLST every thing is object as its base scripting language is Python suppose the same. If you create normal variable assignment uses their base objects like int, float, str etc. Each object will be created on JVM with ID, Type (object type) and value. In this post you will get to know about Object orientation for WLST scripts. The four pillars of Object orientation are supported in WLST as well.
- Encapsulation - How you hide within object
- Abstraction - How the object make interact with the world
- Inheritance -Reusing functionalities of a object
- Polymorphism - Dynamism to your methods
Why should I use OOP in WLST?
The best features identified and designed for object oriented programming mainly following:
- Object are natural easy to understand and write
- Objects are more reusable
- Objects are more maintainable
- Objects can be extended
What is class in WLST?
A class is a special user defined data type, which defines how to build a certain kind of objects. These classes also stores some common data itms that are shared by all its instances of the class. Instances are objects that are created which follow the definition given inside of the class.
In simple words class is a blue print for constructing the objects.
Initializer method __init__
The functions that you define within the class are called methods. for all methods must use the first argument as self.
What is 'self' do in WLST?
The Self is object pointer like in C++, Java we have this pointer that refers to the newly created runtime object attributes values.In __init__, self refers to the object currently being created. In WLST classes self used more often than Java. You don't need to give a value for self. When you call the method VM automatically provides the value for self.
What is Instance?
An instance can be created in the main program that is after completing the class block. To create we need the className followed by perantesis.
i=className()
calling the method can be done with instance period followed by method signature with required parameters.
Magical methods
There are some methods which are executed without calling them! Example initializer method, printing the object into string type using __repr__, and __str__ .When you call the instance in print command it will automaticall invoke these magical methods.
Best practics for class definitons
- While you defining a class the class name must begin with Capital letter
- Explicit inheriting object type give more power to your class
- Define a method like a function. must have a first argument as self which provide access to current instance attribute values
- Better to have a DocString for each method you define in the class
class level methods
static methods
Inheritance
Multiple Inheritance
# This illustrates the subclassing # Empty class with pass # usage of issubclass function class Animal(object): pass class Snake(Animal): def __init__(self): print "Ssss n a k e!!" class Python(Snake): "I like Python class" atr=0 def __init__(self): print "Busssshhhh!!!" Snake.__init__(self) self.atr+=1 def __repr__(self): return 'This is Python object' print "issubclass(Animal,object:)",issubclass(Animal,object) print "issubclass(Snake,Animal:)",issubclass(Snake,Animal) print "issubclass(Python, Snake:)",issubclass(Python, Snake) p=Python() print "Instance printing:", p print "docString:", p.__doc__ print "p is from class:", p.__class__ print "p type:",type(p) print "p id:", id(p) print "p attribute:", p.atr
Private variables in class
In Python classes by default everything is public access. to provide encapsulation for the object double underscore represents the private variables(__var1).
Exercise for Learners
- Create a class for configuring a simple domain with initializer method that will load all properties that required to configure. [managed servers, machines, cluster]
- Create a singleton class that would allow single connection object that can be preserved and used for monitoring JVM, Threads etc.
No comments:
Post a Comment
Please write your comment here