Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA:向方法添加新参数_Java_Methods_Parameters - Fatal编程技术网

JAVA:向方法添加新参数

JAVA:向方法添加新参数,java,methods,parameters,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 las

我有一个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方法参数吗?我仍然是这门语言的初学者。任何帮助都可以。谢谢大家

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

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

若您有另一个客户需要表中的不同值,您只需创建AddNewUser界面的新变体并使用它即可

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

那么在将来,您将需要重载该方法

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){

}

你唯一需要注意的是:这些都是字符串。。。所以你需要知道正手拍时哪个参数对应哪个字符串…

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

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
    }
公共void foo(字符串…字符串){
if(0

希望这有帮助。

您可以使用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++;
    }
}
public void addNewUser(映射mapInput){
迭代器条目=mapInput.entrySet().Iterator();
int i=0;
while(entries.hasNext()){
Entry thisEntry=(Entry)entries.next();
字符串值=(字符串)thisEntry.getValue();
单元格=行。创建单元格(i);
cell.setCellValue(值);
i++;
}
}

让您的方法接受vararg:String。。。阿格斯。请学一点Java。@EgorZhuk一点是不够的..:DMaybe您的
addNewUser
应该接受某种
NewUserParameters
对象。您可以使用给定上下文中所需的任何参数构建这样的对象,然后将其传递给您的方法。另请参阅“可能的重复”,您可以将参数作为用户对象传递,在其中可以传递任何您想要的参数。如果要添加更多属性,可以通过将这些属性添加到用户类来实现。