invoke()

阅读(691) 标签: 调用, 静态函数,

描述:

调用包中类的静态函数

语法:

invoke(p.c.f,ai,…)

备注:

调用包p中类c的静态函数fp可省略,ai为参数。

参数:

p

包路径

c

类名

f

静态方法名

ai

参数

invoke传递参数时参数类型与JAVA类中参数类型对应如下:

bool

java.lang.Boolean

int

java.lang.Integer

long

java.lang.Long

float

java.lang.Double

decimal

java.math.BigDecimal

date

java.sql.Date

time

java.sql.Time

datetime

java.sql.Timerstamp

string

java.lang.String

blob

byte []

示例:

(一)

package api;

public class Calc01 {

public static Double distance1(Number loc) {

double len = Math.abs(loc.doubleValue());

len = Math.round(len*1000)/1000d;

return Double.valueOf(len);

}

}

 

A

1

-12.34567

2

=invoke(api.Calc01.distance1,A1)

3

=invoke(api.Calc01.distance1, -512)

静态方法distance1计算给定的一个坐标,计算与原点之间的距离,计算结果保留3位小数。在集算器中能够调用的方法,除了必须是静态static外,还必须公开public

 

(二)创建实现ILineInput接口的自定义类LineReaders,以便用户使用文件游标读取各种类型的数据文件。可将文件路径作为参数传递给自定义类,通过流读取文件内容,实现单行读取记录、跳过单行和关闭游标的方法

1)示例代码如下:

package api;

 

import java.io.BufferedReader;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import com.raqsoft.dm.ILineInput;

import com.raqsoft.dm.UserUtils;

import com.raqsoft.dm.cursor.ICursor;

 

public class LineReaders implements ILineInput {

private String pathName;

private boolean hasTitle;

private BufferedReader bfr;

 

final String COL_SEP = "\t";

final String encoding = "UTF-8";

 

String lineTxt = null;

final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String[] rowData;

Object[] datas = new Object[3];

 

public LineReaders(String pathName,boolean hasTitle) {

this.pathName = pathName;

this.hasTitle=hasTitle;

}

 

public static LineReaders newInstance(String pathName,boolean hasTitle) {

return new LineReaders(pathName,hasTitle);

}

 

private BufferedReader getBufferedReader() throws IOException {

if (bfr == null) {

File file = new File(pathName);

if (file.isFile() && file.exists()) { // 判断文件是否存在

InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式

bfr = new BufferedReader(read);

}

}

return bfr;

}

//读取单行

public Object[] readLine() throws IOException { 

BufferedReader bfr = getBufferedReader();

try {

if (!hasTitle) {

datas[0] = "编号";

datas[1] = "出生日期";

datas[2] = "姓名";

}

while ((lineTxt = bfr.readLine()) != null) {

rowData = lineTxt.split(COL_SEP);

if (rowData != null && rowData.length == 3) {

if (hasTitle) {

datas[0] = rowData[0];

datas[1] = rowData[1];

datas[2] = rowData[2];

hasTitle = false;

return datas;

}

datas[0] = Integer.parseInt(rowData[0]);

datas[1] = df.parse(rowData[1]);

datas[2] = rowData[2];

return datas;

}

}

return null;

} catch (EOFException e) {

return null;

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return datas;

}

//ILineInput实例转换为游标

public static ICursor UserUtilsReader(String pathName,boolean hasTitle){

return UserUtils.newCursor(newInstance(pathName,hasTitle), "t");

}

//跳过单行

public boolean skipLine() throws IOException{

BufferedReader bfr = getBufferedReader();

try { 

String s = bfr.readLine();

if(s!=null){ 

return true;

}

} catch (EOFException e) {

return false;

} 

return false;

}

//关闭游标

public void close() throws IOException {

if (bfr != null) {

bfr.close();

bfr = null;

}

}

}

2)将此类文件LineReaders.class放至【集算器安装根目录】/classes下的api文件夹中

3)游标读取的文件(格式为UTF-8)内容如下:

4)在集算器网格文件中通过invoke()函数调用自定义函数:

 

A

 

1

=invoke(api.LineReaders.UserUtilsReader,"D://abc.txt",true)

调用自定义函数

2

=A1.skip(3)

3

3

=A1.fetch()