2

私はこの方法を持っています:

public void testJSNI2(){
  String x = "test";
}

私はこのようにこのメソッドにアクセスできます:

helloJsni.@com.jsni.client.HelloJSNIImpl::testJSNI2(Ljava/lang/String;)

xしかし、メソッド内で定義されている文字列にアクセスするにはどうすればよいですか?

4

2 に答える 2

5

答えは正しくありません。JavaScriptとJavaは同じようには機能しません。人はJSNIの助けを借りてjsから任意のフィールドにアクセスできます:

public class JSNIExample {

  String myInstanceField;
  static int myStaticField;

  void instanceFoo(String s) {
    // use s
  }

  static void staticFoo(String s) {
    // use s
  }

  public native void bar(JSNIExample x, String s) /*-{
    // Call instance method instanceFoo() on this
    this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);

    // Call instance method instanceFoo() on x
    x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);

    // Call static method staticFoo()
    @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);

    // Read instance field on this
    var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField;

    // Write instance field on x
    x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + " and stuff";

    // Read static field (no qualifier)
    @com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
  }-*/;

}

これはここで見ることができます:http ://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html

于 2015-02-10T18:04:40.613 に答える
4

x変数はメソッドのスコープ内にあるため、Javaコードでアクセスできないのと同じように、変数にアクセスできません。

于 2012-04-03T17:19:15.260 に答える