我有这个方法:
public void testJSNI2(){
String x = "test";
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样访问这个方法:
helloJsni.@com.jsni.client.HelloJSNIImpl::testJSNI2(Ljava/lang/String;)
Run Code Online (Sandbox Code Playgroud)
但是如何访问在x方法中定义的 String呢?
小智 5
答案是不正确的。JavaScript 和 Java 的行为不同。在 JSNI 的帮助下,Person 可以从 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";
}-*/;
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到这个:http : //www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html