JAVA:向方法

时间:2017-02-23 09:33:12

标签: java methods parameters

我有一个方法addNewUser,它询问用户的用户名,电子邮件和密码。这些将存储在相应列上的Excel工作表中。请参阅以下代码:

public class ExcelConfig {



public FileInputStream fis;
public FileOutputStream fos;
public XSSFWorkbook wb;
public XSSFSheet sh;
public XSSFRow row;
public XSSFCell cell;

int lastRow = 0;

ConfigReader cr = new ConfigReader();

public ExcelConfig(){

      try {
        fis = new FileInputStream(cr.getExcelPath());
        wb = new XSSFWorkbook(fis);
       } 
       catch (Exception e) {
        System.out.println("Error at ExcelConfig " + e.getMessage());
       }


}


public void addNewUser(String un, String em, String pw){


      cell = row.createCell(0);
      cell.setCellValue(un);

      cell = row.createCell(1);
      cell.setCellValue(em);

      cell = row.createCell(2);
      cell.setCellValue(pw);


}

}

现在,在其他类中,我调用了addNewuser方法。见下面的代码

public class NextStepTest {

WebDriver driver;

ConfigReader cf;
ExcelConfig ec;
UserDetails ud;

LandingPage lp;
RegisterPage rp;

String username;
String email;
String password;




@BeforeTest
public void setUp(){
    cf = new ConfigReader();
    ec = new ExcelConfig();
    ud = new UserDetails();


    driver = cf.getChromeDriver();
    driver.get(cf.getAppUrl());


}

@Test
public void register(){

    username = cf.getUsernameFromProperty();
    email = cf.getEmailFromProperty();
    password = ud.getPassword();

    try{

        lp = new LandingPage(driver);
        lp = PageFactory.initElements(driver, LandingPage.class);

        lp.locateRegisterLink();
        lp.clickRegisterLink();

        rp = new RegisterPage(driver);
        rp = PageFactory.initElements(driver, RegisterPage.class);


        rp.locateUsernameField();
        rp.registerAccount(username, email, password);


        ec.getSheet(0);
        ec.getNextRow();
        ec.addNewUser(username, email, password); // here I call addNewUser method that now has username, email and password values
        ec.closeFile();

        rp.logout();
        lp.locateLoginLink();



    }
    catch(Exception e){
        System.out.println("Error under NextStepTest.java => " + e.getMessage());

    }



}

现在,我想问的是,是否可以使addNewUser方法参数动态化?我知道这种情况与项目有关。某些项目可能只需要在excel上添加用户名,电子邮件和密码。如果在其他未来的项目中,它需要添加一个帐户类型怎么办?所以参数现在变为4.或者其他项目只需要电子邮件和密码。我应该每次都更新addNewUser方法参数吗?我仍然是这种语言的先驱。任何帮助都可以。谢谢大家!

4 个答案:

答案 0 :(得分:1)

您可以使用map作为参数。这将帮助您动态传递参数。

 public void addNewUser(Map<String, Object> mapInput){
    Iterator entries = mapInput.entrySet().iterator();
    int i=0;
    while (entries.hasNext()) {
        Entry thisEntry = (Entry) entries.next();
        String value = (String)thisEntry.getValue();
        cell = row.createCell(i);
        cell.setCellValue(value);
        i++;
    }
}

答案 1 :(得分:0)

您可以创建一个接口AddNewUser,它有一个方法createUser,它返回例如创建的XSSFRow行。

然后创建该接口的实现,例如。 AddUserWithEmail,它在构造函数中获取特定值,然后在createUser方法中将用户和电子邮件添加到该行。

如果您的另一个客户在表中需要不同的值,您只需创建一个AddNewUser接口的新变体并改为使用它。

答案 2 :(得分:0)

  

如果在将来的其他项目中,需要添加   帐户类型?

然后将来你需要重载方法

public void addNewUser(String un, String em, String pw){

}

//only username
public void addNewUser(String un){

}

//with all the info + token
public void addNewUser(String un, String em, String pw, String token){

}

你需要注意的唯一事情是:那些都是字符串...所以你需要正确地知道字符串是哪个参数......

答案 3 :(得分:0)

是的,这是可能的。通过使用thi变量参数:

public void foo(String... strings){
    // method body
}

所以使用这个功能你可以打电话:

foo("name");
foo("name", "account");
foo("name", "account", "age");

等...

使用这样的论点:

public void foo(String... strings) {
        if (0 < strings.length()) {
            String name = strings[0];
        }
        if (1 < strings.length()) {
            String account = strings[1];
        }
        if (2 < strings.length()) {
            String account = strings[2];
        }
        // etc
    }

希望这有帮助。

相关问题