What is the Difference Between Type and Class in

A
detailed description of both Types and Classes is
below.


Problem:
What is the difference between a Type and a Class in
LotusScript?

Solution:
A detailed description of both Types and
Classes is below.

I. TYPES

In LotusScript, data types can be
defined with data members (variables) that
can be manipulated as a
single
unit. Types can be used to build a record structure to store database records
within LotusScript.

A type is defined with the TYPE...END
TYPE statement. Within this statement,
the type members are
declared
(without the DIM statement).

For example, to declare a type named
OrderInfo, with the members ID (a
fixed-length string),
CustomerName (a variable length string) and TotalPrice (a currency value); the
following
statements could be
used:

TYPE OrderInfo
ID AS STRING *
6
CustomerName AS STRING
TotalPrice AS CURRENCY
END TYPE

After
declaring a type, a variable can be declared of this type, just as a

variable could be declared of a built-in
data type. For example, to
declare a variable to contain a single order:

DIM OrderVariable AS
OrderInfo

A fixed array of 10 orders can also be declared:

DIM
OrderArray (10) AS OrderInfo

Each of the ten elements of this array is an
instance of the OrderInfo type.
Each of these instances includes
one of
each type member:

ID
CustomerName
TotalPrice

Members of a type can have any built-in data type to another user-defined type;
however,
a type member
cannot contain an instance of itself.

Use Dot Notation
to refer to a member of a type variable. For example, in the
type defined
above, the
members of the instance OrderVariable can be accessed as
follows:

DIM OrderVariable AS OrderInfo
OrderVariable.ID =
"001"
OrderVariable.CustomerName = "Joe Jones"
OrderVariable.TotalPrice =
35.00

II. CLASSES

Classes are similar to types but classes go one
step further. Like types,
classes can declare aggregates of data
which can be manipulated as a single unit. Classes also allow subprograms to be
declared which manipulate
the class data. The data and the subprograms
together form a class; a single
unit. A programming language
such as
LotusScript which allows programs based on classes to be created is
called
an object-oriented
programming language.

A class is defined similar to
a type. Variables are defined which are
aggregated in the class. The
subprograms
or methods can also be defined that manipulate the class
variables or data
members. Methods can take the
form of subs, functions
or properties.

Once a class is defined, the instances of the class are
created. These
instances are called objects. The
CLASS...END CLASS
statement is used to create a class definition. Classes can
only be defined
at the
module level.

For example, in the following script, a Customer
class is declared with data
members called CustName,
Address and Balance.
This class also includes a subprogram member, called

CheckOverdue.

CLASS Customer
PUBLIC CustName AS STRING
PUBLIC
Address AS STRING
Balance AS CURRENCY

SUB CheckOverdue 'This
subprogram takes no arguments
IF Balance > 0 THEN
PRINT "Overdue
Balance"
END IF
END SUB
END CLASS

Private versus
Public:

Class members can be Public or Private. By default, class data
members are
Private and class subprogram
members (methods) are Public.
Public class members can be referred to outside
of the class definition;
Private
members cannot. Private data members are hidden from subprograms
defined
outside of the class. Data hiding
helps programmers structure
their programs to limit access to member data. In
the above example, the
only
subprograms which can refer to Balance data are the member subprograms

(methods) defined in the class
Customer; for example, the member
subprogram CheckOverdue defined above. The
data members
CustName and
Address are declared Public, so these data members can be referred
to by
subprograms
defined outside of the class Customer.

After defining a
class, the DIM statement may be used to declare variables
which refer to
objects of that class.
When an object reference variable is declared, an
object itself is not created.
The object reference variable
is
initialized to contain the special value NOTHING.

For
example:

DIM X AS Customer
'This statement declares an object
reference variable

X can hold either references to Customer objects or
the value NOTHING. X is
initialized to NOTHING.

Once an object
reference variable has been created, an object can be created in
one of two
ways. The first is
to use the keyword NEW in the declaration statement for
the object reference
variable. Using the keyword
NEW declares an object
reference variable, creates an object and assigns a
reference to the
newly-created
object to the variable.

For example:

DIM X AS NEW
Customer

The second way to create an instance of a class is to use a SET
statement which
includes the NEW keyword
and a variable which has been
previously declared as an object reference
variable for that
class.

For example:

DIM X AS Customer
SET X = NEW
Customer

Once an object has been created, Dot Notation may be used to
refer to the data
members and methods
associated with the object of the
given class; similar to using Dot Notation
with a type. Dot Notation is used
to
refer to an individual member of an object. Dot Notation is also used to

reference member subprograms
(methods) in a class.

For objects
declared outside the class, Dot Notation can only be used to access
data
members that are
Public. For example, if X is a Public object reference
variable of the class
Customer, the Public data member
Balance in the
class Customer is referred to as X.Balance outside the class
definition. If
the data member
Balance was not declared using the PUBLIC keyword, it would
be accessible only
from within the class
definition; not outside of
it.

For example:

CLASS Customer
PUBLIC CustName AS
STRING
PUBLIC Address AS STRING
PUBLIC Balance AS CURRENCY

SUB
CheckOverdue ' This subprogram takes no arguments
IF Balance > 0
THEN
PRINT "Overdue Balance"
END IF
END SUB
END CLASS

DIM X
AS NEW Customer
'This statement declares an object reference variable X of
the class Customer
and also creates an object of
that class and assign X
a reference to the new Customer object.

X.CustName = "Acme
Corp."
'This statement is legal because CustName is Public.

X.Balance
= 35.00
'This statement is legal because Balance is
Public.

X.CheckOverdue
'This statement invokes the member subprogram
CheckOverdue. Since this sub
takes no arguments, no
arguments are passed
when the sub is called. In this instance, the text
"Overdue Balance" would
print since
X.Balance is greater than zero.

Members of the Current
Object within a class subprogram (method) can be
declared by using the
ME
keyword. The ME keyword is not valid outside of a class method
(subprogram).

For example:

CLASS Customer
PUBLIC CustName AS
STRING
PUBLIC Address AS STRING
PUBLIC Balance AS CURRENCY

SUB
CheckOverdue 'This subprogram takes no arguments
IF ME.Balance > 0
THEN
PRINT ME.Balance
ELSE
PRINT "No overdue Balance"
END IF
END
SUB
END CLASS

DIM X AS NEW Customer
'This statement declares an
object reference variable X of the class Customer.
It also creates an object
of that
class and assign X a reference to the new Customer
object

X.CustName = "Acme Corp."
'This statement is legal because
CustName is Public.

X.Balance = 35.00
'This statement is legal because
Balance is Public.

X.CheckOverdue
'This statement invokes the member
subprogram CheckOverdue. Since this sub
takes no arguments, no
arguments
are passed when the sub is called. In this instance, the value of
X.Balance
would print since
X.Balance is greater than zero.

For additional
information about Types and Classes, refer to the LotusScript
Reference
Guide, Chapters 3
and 5, respectively.
posted @ 2012-02-01 10:38  hannover  阅读(376)  评论(0编辑  收藏  举报