【问题标题】:example using rhino's JavaAdapter使用 rhino 的 JavaAdapter 的示例
【发布时间】:2014-04-16 03:27:46
【问题描述】:

谁能给我一个例子,说明如何使用 Rhino 的 java 适配器在 java 脚本中扩展 java 类?

【问题讨论】:

标签: java scripting rhino adapter


【解决方案1】:

对于可能遇到此问题的任何其他人,有一个不错的示例here(作者使用它来扩展awt.Canvas)。

var smileyCanvas = new JavaAdapter(awt.Canvas, {
   paint: function (g) {
       var size = this.getSize();
       var d = Math.min(size.width, size.height);
       var ed = d / 20;
       var x = (size.width - d) / 2;
       var y = (size.height - d) / 2;
       // draw head (color already set to foreground)
       g.fillOval(x, y, d, d);
       g.setColor(awt.Color.black);
       g.drawOval(x, y, d, d);
       // draw eyes
       g.fillOval(x+d/3-(ed/2), y+d/3-(ed/2), ed, ed);
       g.fillOval(x+(2*(d/3))-(ed/2), y+d/3-(ed/2), ed, ed);
       //draw mouth
       g.drawArc(x+d/4, y+2*(d/5), d/2, d/3, 0, -180);
   }
});

more information on MDN,包括简要说明和调用语法示例。

【讨论】:

    【解决方案2】:

    取决于你想要继承的什么。我发现如果我将 JavaScript 原型用于对象“定义”,我只会得到 Java 对象的静态方法:

    function test() {
      this.hello = function() {
        for(var i in this) {
          println(i);
        }
      };
    }
    test.prototype= com.acme.app.TestClass; // use your class with static methods
    // see the inheritance in action:
    var testInstance=new test();
    test.hello();
    

    然而,JavaScript 也允许您对对象实例进行原型分配,因此您可以使用这样的函数,并获得更类似于 Java 的继承行为:

    function test(baseInstance) {
      var obj = new function() {
        this.hello=function() { 
          for(var i in this) { 
            println(i); 
          }
        };
      };
      obj.prototype=baseInstance; // inherit from baseInstance.
    }
    
    // see the thing in action:
    var testInstance=test(new com.acme.TestClass()); // provide your base type
    testInstance.hello();
    

    或者在对象本身中使用类似于上述函数的函数(例如init):

    function test() {
      this.init=function(baseInstance) {
        this.prototype=baseInstance;
      };
      this.hello=function() {
        for(var i in this) {
          println(i);
        }
      };
    }
    
    var testInstance=new test();
    println(typeof(testInstance.prototype)); // is null or undefined
    testInstance.init(new com.acme.TestClass()); //  inherit from base object
    
    // see it in action:
    println(typeof(testInstance.prototype)); // is now com.acme.TestClass
    testInstance.hello();
    

    【讨论】:

    • 这与问题无关。他说的是 rhino 的 JavaAdapter
    【解决方案3】:

    由于我不是 100% 确定 Java 适配器的意思是我认为的那样,因此可以使用属性列表 (name = function()) 创建 Java 接口等:

    var runnable = new java.lang.Runnable({
       run: function() { println('hello world!'); } // uses methodName: implementationFunction convention
    };
    java.awt.EventQueue.invokeLater(runnable); // test it
    

    或者对于像这样的单一方法的事情:

    function runnableFunc() { println('hello world!'); }
    var runnable = new java.lang.Runnable(runnableFunc); // use function name
    java.awt.EventQueue.invokeLater(runnable); // test it
    

    【讨论】:

    • 你怎么能像这样扩展另一个类?你不需要JavaAdapter吗?
    • prototype,例子请看我的其他回答。
    • 除非我弄错了,否则这实际上并没有扩展类;它不会像 JavaAdapter 那样创建从另一个类继承的 Java 类。我错过了什么吗?
    【解决方案4】:

    例如在 Rhino 中启动一个线程:

    function startThread(funcOfThread) {
        var someClass = { run: funcOfThread }; // Rhino syntax for new class with overwritten method
        var r = new java.lang.Runnable(someClass);
        var t = new java.lang.Thread(r);
        t.start();
    }
    
    function UDP_Client() {
        while (1)
            java.lang.Thread.sleep(100);
    }
    
    startThread(UDP_Client);
    

    【讨论】:

      猜你喜欢
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2012-10-23
      • 1970-01-01
      • 2012-11-05
      相关资源
      最近更新 更多