创建函数
关于函数语法,LotusScript 和 Java 有两个主要的不同之处。首先,返回类型放在函数名前面(这与 LotusScript 相反,在 LotusScript 中返回类型放在末尾);第二,Java 使用返回关键字从函数返回值(请参见清单 1)。
清单 1. 创建函数示例
return _type function_name(parameter_type 1 parameter_name 1,
parameter_type2 parameter_name2)
{
// 函数代码 ...
return return_type_object;
}
Java |
LotusScript |
public int product(int x, int y)
{
return x*y;
} |
|
Function product(x As Integer, y As Integer) As Integer
product = x * y
End Function
|
Calling the function:
int i = product(2,4);
System.out.println(i);
|
Calling the function:
Dim i As Integer
i = product(2, 4)
Print i
|
创建类
Java 中创建类的语法与 LotusScript 中的类似。它们都使用带有 private/public 选项的类关键字,并且都支持类成员以及类方法。但是,需要注意 LotusScript 类构造方法使用 New 关键字,而 Java 使用无参数的类名称(请参见清单 2 和表 4)。
清单 2. 创建类示例
class class_name
{
type class_member1;
type class_member2;
....
class _name() // 构造方法
{
// 构造方法代码
}
return _type class_method1 (parameter list)
{
// 方法代码
}
return _type class_method2(parameter list)
{
// 方法代码
}
....
}
在IBM Lotus Domino Designer中使用Java构建应用程序(5)
时间:2011-01-24 IBM Oscar Hernandez
表 4. 创建类
Java |
LotusScript |
public class Person
{
private String Name;
private int Age;
public Person()
{
this.Name="";
this.Age=0;
}
public void SetName(String name)
{
this.Name = name;
}
public String GetName()
{
return this.Name;
}
public void SetAge(int age)
{
this.Age = age;
}
public int GetAge()
{
return this.Age;
}
public void AddYears(int i)
{
this.Age = this.Age + i;
}
} |
|
Public Class Person
Private PName As String
Private PAge As Integer
Sub New
PAge = 0
PName = ""
End Sub
Public Property Set Person_Name As String
PName = Person_Name
End Property
Public Property Get Person_Name As String
Person_Name = PName
End Property
Public Property Set Age As Integer
PAge = Age
End Property
Public Property Get Age As Integer
Age = PAge
End Property
Sub AddYears (i As Integer)
PAge = PAge + i
End Sub
End Class
|
创建类的实例 :
Person p = new Person();
p.SetName("John Doe");
p.SetAge(20);
System.out.println(p.GetName() + " " +p.GetAge());
p.AddYears(5);
System.out.println(p.GetName() + " " +p.GetAge());
|
创建类的实例 :
Dim p As New Person
p.Person_Name = "John Doe"
p.Age = 20
Messagebox p.Person_Name & " " & Cstr(p.Age)
p.AddYears(5)
Messagebox p.Person_Name & " " & Cstr(p.Age)
|
|