import java.util.Date;
public class TestNative {
public native void sayhello();
public int number=10;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.loadLibrary("NativeCode");
TestNative tst=new TestNative();
tst.sayhello();
System.out.println(tst.number);
}
}
1、要在C++中修改number属性的值
主要使用的方法有:
jclass class=env->GetObjectClass(object);//得到当前类的class对象
jfieldID id_number=env->GetFieldID(class,"名字",“类型”);
jint number=env->GetIntFiled(class,id_number);
env->setIntField(class,id_number,"参数");
JNIEXPORT void JNICALL Java_jni_TestNative_sayhello(JNIEnv *env, jobject object){
//首先得到jclass对象,是后面得到属性和方法ID的基础
jclass clazz_TestNative=env->GetObjectClass(object);
jfieldID number_id=env->GetFieldID(clazz_TestNative,"number","I");
jint number=env->GetIntField(object,number_id); //得到java中number的值
cout<<"C++ "<<number<<endl;
env->SetIntField(object,number_id,100L);//设置就java中number的值为100
}
2、在C++中如何调用java的方法:
package jni;
import java.util.Date;
public class TestNative {
public native void sayhello();
public int number=10;
public double max(double num1,double num2){
return num1>num2?num1:num2;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.loadLibrary("NativeCode");
TestNative tst=new TestNative();
tst.sayhello();
System.out.println(tst.number);
}
}
对应的C++中的代码:
使用一下的方法
jclass class=env->GetObjectClass(object);
jmethod method=env->GetMethodID(jclass,"方法名",“(参数类型)返回值类型”);
env->Call<TYPE>Method(jclass,jmethod,参数列表);
JNIEXPORT void JNICALL Java_jni_TestNative_sayhello(JNIEnv *env, jobject object){
//首先得到jclass对象,是后面得到属性和方法ID的基础
jclass clazz_TestNative=env->GetObjectClass(object);
jmethodID max_id=env->GetMethodID(clazz_TestNative,"max","(DD)D");
jdouble maxvalue=env->CallDoubleMethod(object,max_id,3.14,3.15);
cout<<maxvalue<<endl;
}
3、在C++调用类中其它对象的方法和属性
调用Father类中的方法
package jni;
public class Father {
public void function(){
System.out.println("Father ...");
}
}
package jni;
public class Child extends Father{
@Override
public void function() {
System.out.println("Child ...");
}
}
package jni;
import java.util.Date;
public class TestNative {
public native void sayhello();
public int number=10;
public double max(double num1,double num2){
return num1>num2?num1:num2;
}
Father p=new Child();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.loadLibrary("NativeCode");
TestNative tst=new TestNative();
tst.sayhello();
System.out.println(tst.number);
}
}
对应的C++代码
JNIEXPORT void JNICALL Java_jni_TestNative_sayhello(JNIEnv *env, jobject object){
//首先得到jclass对象,是后面得到属性和方法ID的基础
jclass clazz_TestNative=env->GetObjectClass(object);
jfieldID p_id=env->GetFieldID(clazz_TestNative,"p","Ljni/Father;");
jobject p=env->GetObjectField(object,p_id); 得到Father对象
jclass p_father=env->FindClass("jni/Father"); 得到Father对象的class对象
jmethodID function_id=env->GetMethodID(p_father,"function","()V"); 得到Father对象的MethodID
env->CallVoidMethod(p,function_id); 调用Child 的function方法 提现了java中的多态
env->CallNonvirtualVoidMethod(p,p_father,function_id); 调用Father中的function方法
}
结果
Child ...
Father ...
10