2017-05-07 94 views
1

我使用此代碼來驗證文件的數字符號,構造函數打印文件的上下文,但我想知道如何將打印保存爲變量,因爲構造函數僅使用「打造」對象,這是代碼:從構造函數中獲取一個變量

public class VerifyMessage { 
    private List<byte[]> list; 

    @SuppressWarnings("unchecked") 
    // The constructor of VerifyMessage class retrieves the byte arrays from the File and prints the message only if the signature is verified. 
    public VerifyMessage(String filename, String keyFile) throws Exception { 
     ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename)); 
     this.list = (List<byte[]>) in.readObject(); 
     in.close(); 

     System.out.println(verifySignature(list.get(0), list.get(1), keyFile) ? "VERIFIED MESSAGE" + "\n----------------\n" + new String(list.get(0)) : "Could not verify the signature.");  
    } 

我怎樣才能‘拯救構造的外部’的System.out.println作爲全球成員字符串變量? 提前致謝

+0

嗯......你可以一個新的'String'字段添加到類和'VerifyMessage分配給它()'。順便說一下,Java編碼約定聲明我們不會爲第一個字母命名使用大寫字母的方法。 –

回答

2

您的課程已經包含一個列表字段。

與此類似,您聲明一個字符串字段 - 然後您只需將當前正在打印的值分配給該字段。

0
public class VerifyMessage { 
private List<byte[]> list; 

Object variable = null; 


public VerifyMessage(String filename, String keyFile) throws Exception { 
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename)); 
    this.list = (List<byte[]>) in.readObject(); 
    in.close(); 


    this.variable = this.verifySignature(list.get(0), list.get(1), keyFile); 
} 


private Object verifySignature(byte[] bs, byte[] bs2, String keyFile) { 

    // 
    return null; 
} 

}

相關問題