Creating Custom Classes in LotusScript, Part 1

xiaoxiao2021-03-06  39

The LotusScript language in Domino is very powerful once you find out that you can create custom classes with it. I'm going to write some articles about creating great helper classes, and start off with the basics of creating user classes. I am NOT going To Explain Why Object Oriented Programming is Better Than Procedure Programming ...

The Definition of a class looks like this:

Public Class Car

END CLASS

The Public tells that this class will be seen outside the agent or scriptlibrary, where the class is created You can also make a Private class, that only the agent or script library can see, where the class is defined.:

Private Class Car

END CLASS

To create an instance of a class, you use the new keyword:

DIM Mycar as new car ()

OR:

Dim mycar as car

Set mycar = new car ()

To add functionality to a class, you can create functions, subroutines, property getters and property getters. I will not explain the last ones, as they easily can be transformed into functions and subs. To add a function, that returns a value, you Write Like:

Public Class Car

Public function gettopspeed () AS Integer

END FUNCTION

END CLASS

To Add A Subroutine, That Does Not Return A Value, You Write:

Public Class Car

Public Sub Start ()

End Sub

END CLASS

There area, delete and new:

Public Class Car

Public Sub New ()

End Sub

Public SUB Delete ()

End Sub

END CLASS

The New Subroutine, Also Called Constructor, IS Called WHENEVER You Use the New Keyword To Create A New Instance of The Cree. The delete subroutine is caled....................

To Add Arguments That Can Be Passed To Subroutines and Functions, Just Add Them Like You Do To Any Ordinary Function Or Subroutine:

Public Class Carpublic Sub New (Topspeed AS Integer)

End Sub

END CLASS

To Add Instance Member Variables, Declare The USUAL DIM:

Public Class Car

TOPSPEED AS INTEGER

END CLASS

To use instance member variables, you handle them like any other variable. A special case is show below, when a function has an argument variable, that has the same name as the instance variable. The keyword Me is used to denote the current object, And whening me.topspeed, we mean the instance member variable topspeed, and not the function argument variable:

Public Class Car

TOPSPEED AS INTEGER

Public Sub New (TopSpeed ​​AS Integer)

Me.topspeed = TOPSPEED

End Sub

Public function gettopspeed () AS Integer

Gettopspeed = TOPSPEED

'CAN Also Be Written As:

'Me.gettopspeed = me.topspeed

END FUNCTION

END CLASS

To Remove An Object from Memory, You Use The Delete Keyword. This Removes The Actual Object from Memory, And All References That Are.com Memory, And All References That Are.com

DIM Mycar as new car (120)

Delete mycar

If you just want to relative a reference to an Object, you use the Nothing Keyword. This Will Only Remove The Specified Reference, But The Object Is Still in Memory, UnTil All References Are Removed.

DIM Mycar as new car (120)

SET mycar = Nothing

You can test if an object reason is still valid, by Comparing the Object Reference to Nothing:

DIM Mycar as new car (120)

'Do some work ...

IF mycar is nothing _

Then Error 2000, "Object Reference Not Valid"

To Check What Type An Object Reference IS, You Can Use The ISA Keyword. This is useful, when you don't know at compile time What type of object you etc::

DIM MyVehi As Variant

IF username = "donald" TENSET MyVEHICLE = New Helicopter ()

Else

SET MyVehi = new bicycle ()

END IF

IF MyVehi ISA "Helicopter" _

Then Print "fly away!"

That if you want to assign Non-Objects to the Same Variable, you must test, before trying to use isa ipes

DIM MyVehi As Variant

IF username = "donald" then

Set myvehicle = new helpter ()

Else if Username = "mickey" then

SET MyVehi = new bicycle ()

Else

MyVehi = "Illegal user?"

END IF

IF isobject (myvehicle) THEN

IF MyVehi ISA "Helicopter" _

Then Print "fly away!"

Else

Print "No Object, Probably Strange User ..."

END IF

TO GET the name of the class thing wage used to create an Object Instance, Use The keyword typeName:

DIM Mycar as new car (120)

'Will Print "Car":

Print "The Type of Object IS:" & TypeName (MyCar) ...

OK, Let's Do Some Useful with our new knowledge, let's create a working car class. The start submet be called before the accelerate () Method is Called.

Public Class Car

TOPSPEED AS INTEGER

Currentspeed As INTEGER

Started As Intger

Public Sub New (TopSpeed ​​AS Integer)

Me.topspeed = TOPSPEED

End Sub

Public Sub Start ()

Started = TRUE

End Sub

Public Sub Stop ()

Currentspeed = 0

Started = false

End Sub

Public Function Accelerate () AS Integer

IF not start_

Then Error 2000, "Start The Before Driving"

IF currentspeed => TOPSPEED _

THEN Error 2000, "Top Speed ​​Reached"

Currentspeed = CurrentSpeed ​​ 10

Accelerate = CurrentSpeed

END FUNCTION

Public function gettopspeed () as integergettopspeed = TOPSPEED

END FUNCTION

Public function getcurrentspeed () AS Integer

Getcurrentspeed = currentspeed

END FUNCTION

END CLASS

To use our class, we can write:

DIM Car As New Car (120)

Call car.start ()

Do while car.getcurrentspeed ()

Print "Current Speed ​​IS:" & Car.accelerate ()

Loop

Print "Top Speed" & Car.gettopspeed () & "Reached!"

Call car.stop ()

Print "car parked!"

Set car = Nothing

There is more to learn About classes in LotusScript, So Stay Tuned for the next article! MeanWhile, Check The Following Links:

"Using the object-oriented features of LotusScript" on Lotus Developer Domain, great explanation of all (?) Advantages! "User-defined classes" in the Domino Designer Help 5.0.3The Unfinished LotusScript Book by Julian RobichauxBill Buchan's Lotus Notes programming tips

转载请注明原文地址:https://www.9cbs.com/read-72578.html

New Post(0)