(一)取得Java属性/设定Java属性值
1.取得相应属性的jfieldID之后就可以用
Set<TYPE>Field();
Get<TYPE>Field();
SetStatic<TYPE>Field();
GetStatic<TYPE>Field();
等函数对java属性进行操作b C++源码部分:
2.获取数组属性——>GetObjectField
3.例子:
a java 代码部分:
view plain copy to clipboard print ?
- package cn.itcast;
-
-
-
-
- import java.util.Date;
-
- public class TestNative {
- public native void sayHello();
-
- public int number = 10;
- public static void main(String[] args) {
- System.loadLibrary("nativeCode");
-
- 中加载DLL,然后调用声明的native方法
- TestNative tst=new TestNative();
- tst.sayHello();
- System.out.println(tst.number);
- }
-
- }
b C++源码部分
view plain copy to clipboard print ?
- #include "cn_itcast_TestNative.h"
- #include<iostream>
- using namespace std;
-
-
- JNIEXPORT void JNICALL Java_cn_itcast_TestNative_sayHello
- (JNIEnv *env, jobject obj){
-
-
- jclass clazz_TestNative = env->GetObjectClass(obj);
- jfieldID id_number = env->GetFieldID
-
- (clazz_TestNative,"number","I");
- jint number = env->GetIntField(obj,id_number);
-
- cout<<number<<endl;
-
- env->SetIntField(obj,id_number,100L);
- }
(二)Java方法的调用
1.取得相应的jmethodID传入函数的参数中,就可以用
Call<TYPE>Method();
CallStatic<TYPE>Method();
CallNonvittual<TYPE>Method();
等函数对java函数进行调用
2.调用形式
java中方法:
boolean function(int i , bouble d , char c){
···
}
//env->CallBooleanMethod(obj , id_function, 100L, 3.44 , L'3');//第一种调用函数形式
//第二种调用形式
jvalue * args = new jvalue[3];//存储参数的数组
args[0].i=100L;
args[1].d=3.44;
args[2].c=L'3';
env->CallBooleanMethod(obj , id_function , args);
delete [] args;//删除内存
3.调用例子
a java代码部分
view plain copy to clipboard print ?
- package cn.itcast;
-
- import java.util.Date;
-
- public class TestNative {
- public native void sayHello();
- double max(double num1,double num2){
- return num1>num2?num1:num2;
- }
-
- public static void main(String[] args) {
- System.loadLibrary("nativeCode");
-
- 中加载DLL,然后调用声明的native方法
- TestNative tst=new TestNative();
- tst.sayHello();
- }
-
- }
b C++源码部分
view plain copy to clipboard print ?
- #include "cn_itcast_TestNative.h"
- #include<iostream>
- using namespace std;
-
-
- JNIEXPORT void JNICALL Java_cn_itcast_TestNative_sayHello
- (JNIEnv *env, jobject obj){
-
- jclass clazz_TestNative = env->GetObjectClass(obj);
- jmethodID id_max=env->GetMethodID
-
- (clazz_TestNative,"max","(DD)D");
- jdouble maxvalue=env->CallDoubleMethod(obj,
-
- id_max,3.14,3.15);
- cout<<maxvalue<<endl;
-
-
- }
3.CallNonvirtual<TYPE>Method
如下java代码:
第一段
public class Father{
public void function(){
System.out.println("Father:func");
}
}
第二段
public class Child extends Father{
public void function(){
System.out.println("Child:func");
}
}
问题:如果出现以下代码,问其是调用哪个方法?
Father p = new Child();
p.function();
如下C++代码:
第一段
class Father{
public:
virtual void function(){//若加上virtual又如何
cout<<"Father:func"<<endl;
}
}
第二段
class Child: public Father
public: void function(){
cout<<"Child:func"<<endl;
}
}
问题:如果出现以下代码,问其是调用哪个成员函数?
Father* p = new Child();
p->function();
在JNI中定义的CallNonvirtual<TYPE>Method就能够对子类对象调
用父类方法的功能。如果想要调用一个对象的父类方法,而不是子类
的这个方法的话,就可以使用CallNonvirtual<TYPE>Method
使用方式:
首先取得父类及要调用的父类方法的jmethodID
然后传入到这个函数就能通过子类对象呼叫被覆写(override)的父
类的方法
使用实例
(1)新增Father类
view plain copy to clipboard print ?
- package cn.itcast;
-
- public class Father {
- public void function(){
- System.out.println("Father:function");
- }
- }
(2)新增Child类
view plain copy to clipboard print ?
- package cn.itcast;
-
- public class Child extends Father {
-
- public void function() {
- System.out.println("Child:function");
- }
- }
(3)java代码部分
view plain copy to clipboard print ?
- package cn.itcast;
- import java.util.Date;
- public class TestNative {
- public native void sayHello();
- public Father p = new Child();
- public static void main(String[] args) {
- System.loadLibrary("nativeCode");
-
- 中加载DLL,然后调用声明的native方法
- TestNative tst=new TestNative();
- tst.sayHello();
- }
- }
(4)C++源码部分
view plain copy to clipboard print ?
- #include "cn_itcast_TestNative.h"
- #include<iostream>
- using namespace std;
-
-
- JNIEXPORT void JNICALL Java_cn_itcast_TestNative_sayHello
- (JNIEnv *env, jobject obj){
-
- jfieldID id_p = env->GetFieldID
-
- (clazz_TestNative,"p","Lcn/itcast/Father;");
- jobject p = env->GetObjectField(obj,id_p);
-
- jclass clazz_Father = env->FindClass
-
- ("cn/itcast/Father");
- jmethodID id_Father_function = env->GetMethodID
-
- (clazz_Father,"function","()V");
-
-
- env->CallVoidMethod(p,id_Father_function);
-
-
-
-
-
-
- env->CallNonvirtualVoidMethod
-
- (p,clazz_Father,id_Father_function);
-
- }