下面有个具体的例子。例子浅而易见,这里同时展示了用函数指针,做钩子进行回调的机制。这东西在java中只能通过接口来实现。  Example:            // Parse the text. If it contains any "NaN" strings, replace them
            // with the NaN value. All other values are left alone.            myData = text.parseJSON(function (value) {
                if (value === 'NaN') {
                    return NaN;
                }
                return value;
            });上面到底在说些什么?parseJSON这个方法是干什么用的,怎么调用这个方法,看不懂啊。。

解决方案 »

  1.   

    什么叫 钩子?那个例子里哪个是指针? 有人能帮我用java写个她说的那个什么“..用ava中只能通过接口来实现..."吗?
      

  2.   

    text.parseJSON(此处是函数指针),即parseJSON方法需要传入一个函数.
    分开写就是 fun = function(){...}; text.parseJSON(fun);
    parseJSON中一定会定义有调用该函数的代码 fun(); 就是回调机制了。
    换句话说,由于fun是变量,定义不同的fun, parseJSON中调用的就是不同的函数。
    这在java中就类似于接口:
    定义接口
    public interface IFunction {
     void fun();
    }public class Fun1 implements IFunction {
    public void fun() {
                 .......
    }
    }public class Fun2 implements IFunction {
    public void fun() {
                 .......
    }
    }public class Text {
    public void parseJSON(IFunction function){
    function.fun();
    }
    }调用
    Fun1 fun1;
    Fun2 fun2;
    fun1 = new Fun1();
    fun2 = new Fun2();
    Text text;
    text = new Text();
    text.parseJSON(fun1);
    text.parseJSON(fun2);
      

  3.   

    貌似这个Example写的不是很好,既然是示例,应当简洁明了才对,参见:
    http://www.blogjava.net/emu/archive/2006/08/08/62318.html