2013-12-18 49 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 
} 
相关问题