王朝网络
分享
 
 
 

Creating Custom Classes in LotusScript, part 1

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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 are two special subroutines, 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 class. The delete subroutine is called when there are no references to the object.

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 Car

Public Sub new(topSpeed As Integer)

End Sub

End Class

To add instance member variables, declare them inside the class, without 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 when saying 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 using the object can not use it any more.

Dim myCar as New Car(120)

Delete myCar

If you just want to relase 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 reference 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 handle:

Dim myVehicle as Variant

If userName = "Donald" Then

Set myVehicle = New Helicopter()

Else

Set myVehicle = New Bicycle()

End If

If myVehicle IsA "Helicopter" _

Then Print "Fly away!"

The problem with the above is, that if you want to assign non-objects to the same variable, you must test if the variable is an object, before trying to use IsA or testing if it is Nothing:

Dim myVehicle as Variant

If userName = "Donald" Then

Set myVehicle = New Helicopter()

Else If userName = "Mickey" Then

Set myVehicle = New Bicycle()

Else

myVehicle = "Illegal user?"

End If

If IsObject(myVehicle) Then

If myVehicle IsA "Helicopter" _

Then Print "Fly away!"

Else

Print "No object, probably strange user..."

End If

To get the name of the class that was 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 sub must 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 started _

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 Integer

getTopSpeed = 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() < car.getTopSpeed()

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.3

The Unfinished LotusScript Book by Julian Robichaux

Bill Buchan's Lotus Notes programming tips

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有