2011-09-19 27 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 = ...; 
     } 
    } 
}