package com.sgai.mes.function;import com.sgai.exp.ExpressionContext;
import com.sgai.exp.math.function.BaseFunction;public class CharLengthFunction extends BaseFunction{ public String getName() {
// TODO Auto-generated method stub
return "CALCULATE_CHAR_LENGTH" ;
} public String perform(ExpressionContext expCtx, String[] arg) {
// TODO Auto-generated method stub

if (arg==null || arg.length==0) return "0";
String outMatId=null;
String targetCtxPath = null;
        //上下文中保存物料号的参数ID
        targetCtxPath = arg[0];
        if (targetCtxPath==null || 
         targetCtxPath.trim().equals("")||
         targetCtxPath.equals("NEX")) {
         targetCtxPath = "mat";
        }
        outMatId = arg[1];
        String length=String.valueOf(outMatId.length());
return length;
}

}
=========================================
CALCULATE_CHAR_LENGTH(mat,mat\matWidth,NEX)   外部是怎么调的;
我看外部传进去了三个参数,public String perform(ExpressionContext expCtx, String[] arg)这里只声明了两个,取前两个还是怎么着,初学,求教啊!!!

解决方案 »

  1.   

    package com.sgai.mes.function;import com.sgai.exp.ExpressionContext;
    import com.sgai.exp.math.function.BaseFunction;public class CharLengthFunction extends BaseFunction{ public String getName() {
    // TODO Auto-generated method stub
    return "CALCULATE_CHAR_LENGTH" ;   --返回本类的名字
    } public String perform(ExpressionContext expCtx, String[] arg) {
    // TODO Auto-generated method stub

    if (arg==null || arg.length==0) return "0"; --如果arg[0]为空或者字符串长度为0,返回0
    String outMatId=null;
    String targetCtxPath = null;
            //上下文中保存物料号的参数ID
            targetCtxPath = arg[0];
            if (targetCtxPath==null || 
             targetCtxPath.trim().equals("")||
             targetCtxPath.equals("NEX")) {
             targetCtxPath = "mat";
            }
            outMatId = arg[1];   --如果targetCtxPath为空或者=""或者="NEX"或者="mat",outMatId=arg[1]
            String length=String.valueOf(outMatId.length()); --求outMatId长度并返回长度值
    return length;
    }

    }这个是我自己写的注释,帮忙看看理解是否有误。
      

  2.   

    package com.sgai.mes.function;import com.sgai.exp.ExpressionContext;
    import com.sgai.exp.math.function.BaseFunction;public class CharLengthFunction extends BaseFunction { public String getName() {
    // TODO Auto-generated method stub
    return "CALCULATE_CHAR_LENGTH" ;   //返回一个字符串"CALCULATE_CHAR_LENGTH"。
    } public String perform(ExpressionContext expCtx, String[] arg) {
    // TODO Auto-generated method stub if (arg==null || arg.length==0)
    return "0"; //如果数组arg为空或者数组arg长度为0,返回字符串"0"。
    String outMatId=null;//初始定义字符串变量outMatId。
    String targetCtxPath = null;//初始定义字符串变量targetCtxPath。
            //上下文中保存物料号的参数ID
            targetCtxPath = arg[0];//给字符串变量targetCtxPath赋值。因为上面以判断字符串数组arg部位空且长度大于0,所以可以这样赋值。
            if (targetCtxPath==null || 
             targetCtxPath.trim().equals("")||
             targetCtxPath.equals("NEX")) {
             targetCtxPath = "mat";//如果字符串变量targetCtxPath为null,或者为"",或者为"NEX"值时,将targetCtxPath赋值为"mat"。
            }
            outMatId = arg[1];   //将outMatId赋值为arg[1],此代码有风险,若传进来的arg的长度为1,此处则会抛出越界异常。
            String length=String.valueOf(outMatId.length()); //是将outMatId的长度值转化为字符串。
            return length;
    }
    }
      

  3.   

    代码是不建议如下这样写的targetCtxPath.trim().equals("")||targetCtxPath.equals("NEX")因为这里面前面有个targetCtxPath==null,所以没问题,若没有对null值的判断,这样的代码是有风险的,应该写为:"".equals(targetCtxPath)||"NEX".equals(targetCtxPath)因为""和"NEX"是确定值,这样就会避免空指针异常