2011-09-19 25 views
0

我有一个C程序进行通信的Java程序。我以前写过JNI,但我的输出结构更简单,C结构只包含双/整数和双精度/整数数组。JNI和访问一个子类

现在我的结构包含一个子(类/子类),我不知道如何更改代码来访问数据的子类/场。

我的C代码看起来像这样,但我如何访问一个值,如DefaultFeeAmount,如果你看看我的Java类下面的代码....我怎么去的子类内的元素?

Ç简单....

{ 
    jclass out_rec_cls = jenv->GetObjectClass(ptrTo_out_rec); 
    jfieldID fldID, fldID2; 
    jintArray arr; 
    jdoubleArray darr; 
    jobjectArray oarr; 
    jsize len;//,len2; 
    jint *arrElems; 
    jdouble *darrElems; 
    jobject *oarrElems; 
    int i; 
    char temp_str[100],temp_str2[10000]; 

    fldID = jenv->GetFieldID(out_rec_cls, "ErrorCode", "I"); 
    if(fldID != NULL) 
     jenv->SetIntField(ptrTo_out_rec, fldID, out_rec->error_code); 
} 

的Java

class FeeOutput { 
    public double DefaultFeeAmount; 
    public double MaximumAmount; 
    public int FeeID; 
    public int CompType; 
    public int Handling; 
    public int CapType; 
    public int ProfitType; 
    public int EffectiveDateMonth; 
    public int EffectiveDateDay; 
    public int EffectiveDateYear; 
    public int VendorBasedFee; 
    public int DealerRequestedFee; 
    public int DealerFixedTranFee; 
    public double FeeAmount; 
    public int FeeCompliant; 
    public String FeeName = ""; 

    public FeeOutput() { 
    } 
} 

public class VFeeOutput { 
    public static final int NUM_FEES = 100; 
    public FeeOutput[] FeeStruct = new FeeOutput[NUM_FEES]; 

    public int ErrorCode; 

    public String ErrorString = ""; 

    public String Version = ""; 

    public VFeeOutput() { 
    } 
} 

回答

0

作为广传播Java约定提示,请以较低的情况下开始的变量名。在这里如何访问Java中的“结构”字段。

public class VFeeOutput { 
    public static final int NUM_FEES = 100; 
    public FeeOutput[] FeeStruct = new FeeOutput[NUM_FEES]; 
    public int ErrorCode; 
    public String ErrorString = ""; 
    public String Version = ""; 
    public VFeeOutput() { 
    } 

    private void loopThoughtFeeOutput() { 
     for(FeeOutput feeOutput : FeeStruct) { 
      feeOutput.CompType = ...; 
     } 
     // or 
     for(int i = 0; i < FeeStruct.length; i++) { 
      FeeStruct[0].CompType = ...; 
     } 
    } 
}