有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从另一个类(数组)调用变量?

我在Eclipse和IUnit中使用Selenium Web驱动程序。我有从excel文件读取数据的代码。每列都表示为数组。这是代码:

class ReadExcel {
ArrayList path_name = new ArrayList();
        ArrayList field_key = new ArrayList();
        ArrayList field_name = new ArrayList();
        ArrayList window_new = new ArrayList();
        ArrayList link = new ArrayList();
        lov_name = new ArrayList();
    public void mai() {
        int i = 0;


        String path_namel = "", field_keyl = "", field_namel = "", window_newl = "", linkl = "", lov_namel = "";
        String filename = "E:/data.xls";
        if (filename != null && !filename.equals("")) {
            FileInputStream fs = new FileInputStream(filename);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            for (int k = 0; k < wb.getNumberOfSheets(); k++) {
                int j = i + 1;
                HSSFSheet sheet = wb.getSheetAt(k);
                int rows = sheet.getPhysicalNumberOfRows();
                for (int r = 1; r < rows; r++) {
                    HSSFRow row = sheet.getRow(r);
                    int cells = row.getPhysicalNumberOfCells();
                    HSSFCell cell1 = row.getCell(0);
                    path_namel = cell1.getStringCellValue();
                    HSSFCell cell2 = row.getCell(1);
                    field_keyl = cell2.getStringCellValue();
                    HSSFCell cell3 = row.getCell(2);
                    field_namel = cell3.getStringCellValue();
                    HSSFCell cell4 = row.getCell(3);
                    window_newl = cell4.getStringCellValue();
                    HSSFCell cell5 = row.getCell(4);
                    linkl = cell5.getStringCellValue();
                    HSSFCell cell6 = row.getCell(5);
                    lov_namel = cell6.getStringCellValue();

                    path_name.add(path_namel);
                    field_key.add(field_keyl);
                    field_name.add(linkl);
                    window_new.add(window_newl);
                    link.add(linkl);
                    lov_name.add(lov_namel);
                }
                i++;
            }
        }
    }
}

在我的硒测试中,我有这样的循环:

for (int i=0; i<path_name.length; i++){
         driver.findElement(By.xpath(path_name[i])).click();
}

这里我使用变量path_name,它是数组,必须等于ReadExcel类中的path_name。实际上我想用excel中的值作为数组。如何从ReadExcel调用变量

编辑 我尝试使用getter和setter方法

int q;
String g;
public String getG() {
    return g;}
public void setG(String g) {
    this.g = g;}
public int getQ() {
    return q;}
public void setQ(int q) {
    this.q = q;}

q=path_name.size();
g=path_name.get(i).toString();

我在测试中用这种方式调用变量

ReadExcel h = new ReadExcel();
String k=   h.getG();
ReadExcel p = new ReadExcel();
int n=  p.getQ();

for (int j=0; j<n; j++){
driver.findElement(By.xpath(k)).click();}

编辑器中没有错误,但循环不起作用。它应该点击链接(k),但没有效果。 我也尝试了这个(在第一个答案中建议)

ReadExcel readExcel = new ReadExcel();
    ArrayList<String> path_name = readExcel.getPath_name();

    for(String pathName: path_name){
        driver.findElement(By.xpath(pathName)).click();
    }

同样的效果。它不会点击链接


共 (1) 个答案

  1. # 1 楼答案

    一种方法是在ReadExcel类中为要访问的变量实现setter()getter()方法。它们显然是公开的方法

    编辑:

    从你试图更新的内容和我的理解来看,你做了很多错事。假设您正在调用另一个类中的最后一段代码,下面是您真正应该做的

    另外,我假设您已经修改了ReadExcel类,使其看起来像这样

    public class ReadExcel {
    
        ArrayList<String> pathName      = new ArrayList<String>();
        ArrayList<String> fieldKey      = new ArrayList<String>();
        ArrayList<String> fieldName     = new ArrayList<String>();
        ArrayList<String> windowNew     = new ArrayList<String>();
        ArrayList<String> link          = new ArrayList<String>();
    
        public ReadExcel() {
            pathName = new ArrayList<String>();
            fieldKey = new ArrayList<String>();
            fieldName = new ArrayList<String>();
            windowNew = new ArrayList<String>();
            link      = new ArrayList<String>();
        }
    
        /**
        * Not so sure of this method name. But make sure that this method is called before
        * you try to call getXX() methods
        */
        public void mai() {
            String filename = "E:/data.xls";
    
            if(fileName == null || "".equals(fileName))
                return;
    
            HSSFWorkbook workBook = null;
            FileInputStream fileInputStream = null;
            HSSFSheet sheet;
            HSSFRow row;
    
            int rows;
    
            try{
                fileInputStream = new FileInputStream(new File(fileName));
                workBook = new HSSFWorkbook(fileInputStream);
    
                for(int sheetIndex = 0; sheetIndex < workBook.getNumberOfSheets(); sheetIndex++){
                    sheet = workBook.getSheetAt(sheetIndex);
    
                    rows = sheet.getPhysicalNumberOfRows();
    
                    for(int rowIndex = 0; rowIndex < rows; rowIndex++){
                        /**
                         * Update with your own logic for retrieval
                         */
                        row = sheet.getRow(rowIndex);
                        if(row.getPhysicalNumberOfCells() < 6)
                            continue;
    
                        pathName.add(row.getCell(0).getStringCellValue());
                        fieldKey.add(row.getCell(0).getStringCellValue());
                        fieldName.add(row.getCell(0).getStringCellValue());
                        windowNew.add(row.getCell(0).getStringCellValue());
                        link.add(row.getCell(0).getStringCellValue());
                    }
                }
    
            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }finally{
                if(fileInputStream != null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                workBook = null;
            }
            }
        }
    
        /**
        * The getter/setter methods for the variables
        */
        public ArrayList<String> getPathName() {
            return pathName;
        }
    
        public void setPathName(ArrayList<String> pathName) {
            this.pathName = pathName;
        }
    
        public ArrayList<String> getFieldKey() {
            return fieldKey;
        }
    
        public void setFieldKey(ArrayList<String> fieldKey) {
            this.fieldKey = fieldKey;
        }
    
        public ArrayList<String> getFieldName() {
            return fieldName;
        }
    
        public void setFieldName(ArrayList<String> fieldName) {
            this.fieldName = fieldName;
        }
    
        public ArrayList<String> getWindowNew() {
            return windowNew;
        }
    
        public void setWindowNew(ArrayList<String> windowNew) {
            this.windowNew = windowNew;
        }
    
        public ArrayList<String> getLink() {
            return link;
        }
    
        public void setLink(ArrayList<String> link) {
            this.link = link;
        }
    }
    

    我希望你在某处调用mai()方法(这个名字听起来很奇怪,尽管)从excel中检索数据并将其存储在ArrayList中,然后再尝试调用以下代码:

    ReadExcel readExcel = new ReadExcel();
    ArrayList<String> path_name = readExcel.getPath_name();
    
    for(String pathName: path_name){
        driver.findElement(By.xpath(pathName).click();
    }
    

    一些指向代码的指针:

    • 使用泛型。而不是放弃^ {< CD6> }考虑,使用^ {CD7>}

    • 如果在编写代码时使用Java命名约定,效果会很好。其中之一是使用混合大小写字母组合方法名称,以小写字母开头,随后的每个单词以大写字母开头。因此,不要使用^ {< CD8> },考虑一些类似^ {CD9}},或者甚至^ {CD10}}(尽管我们大多数人更倾向于第一个)。{a1}