2013-12-18 48 views
0

我在類X中有一個變量,我想將其內容與另一個類Y中的字段的值匹配。
我有X類下面的代碼:將變量中的值與文本字段的值進行比較

String Cellresult; 
Cell a1 = sheet.getCell(columNumber,rowNumber); // gets the contents of the cell of a an excell sheet 
Cellresult = a1.getContents(); // stores the values in the variable named Cellresult 
System.out.println(Cellresult); // prints the values which is working fine till here 

現在,在另一個類Y,我想的Cellresult值與具有ID txtVFNAME已經充滿字段的值進行比較。

我想:

WebElement a = idriver.findElement(By.id("txtVFNAME")); 
if (a.equals(Cellresult)){ 
     System.out.println("text is equal"); 

}; 

我得到的錯誤,甚至在編譯:: Cellresult cannot be resolved to a variable之前。
我使用java,Eclipse,IE 10,贏得8.kindly幫助。非常感謝。

+1

'Cellresult'只在'X'類中知道,使* getter *在'Y'類中使用。請注意,你的命名非常混亂。我建議您遵循[Java命名約定](http://www.oracle.com/technetwork/java/codeconv-138413.html)。 – Maroun

+0

我如何在另一個課程中訪問它? – newLearner

+1

以及..你的Cellresult是一個局部變量..它不會在函數外部可見..你必須使它成爲類X中的一個實例級別變量(將它放在所有函數之外,但放在類中),添加getter和setter在它的類中,你必須創建一個新的X實例,使用getCellResult()來獲得結果。 – TheLostMind

回答

6

您不能只參照類Y中的類X的變量。爲了能夠訪問類X的實例變量,請使用它創建一個實例來訪問它。

X x = new X(); 
if (a.equals(x.Cellresult)){ // Cellresult is public 

但在你的情況下,似乎Cellresult存在於一個方法內而不是一個實例變量。在這種情況下,請從方法中返回您的Cellresult並在此處使用它。

X x = new X(); 
if (a.equals(x.methodThatReturnsCellresult())){ // methodThatReturnsCellresult is public 

您的類X中的方法看起來像這樣。

public String methodThatReturnsCellresult() { 
    // Other stuffs too. 
    String Cellresult; 
    Cell a1 = sheet.getCell(columNumber,rowNumber); // gets the contents of the cell of a an excell sheet 
    Cellresult = a1.getContents(); // stores the values in the variable named Cellresult 
    System.out.println(Cellresult); 
    return Cellresult; // returning the Cellresult value to be used elsewhere, in your case, in Class Y 
} 
相關問題