gpt4 book ai didi

java:如何为变量自动生成自定义方法

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:46 25 4
gpt4 key购买 nike

我有一种情况,我想为我的字段定义各种行为。

class SomePage {
//...

@FindBy(id = "selectEthinicity")
private WebElement ethinicitySelect;

@FindBy(id = "someId")
private WebElement usernameInputField;

@FindBy(id = "buttonSubmit")
private WebElement submitButton;


public void selectEthnicity(String param) throws Exception {
new Select(ethnicitySelect).selectByVisibleText(param);
}

public void selectEthnicityByIndex(String param) throws Exception {
new Select(ethnicitySelect).selectByIndex(Integer.parseInt(param));
}

public int getSelectEthinicityNumberOfOptions() {
new Select(ethnicitySelect).getOptions().size();
}

public void waitForOptionOfSelectEthnicityToLoad() {
//logic to wait for an options of Ethinicity to load
}

public void selectEthnicity_Wait(String param) throws Exception {
//wait
//select Ethinicity
}

public void selectEthnicityByIndex_Wait(String param) throws Exception {
//wait and
//Select Ethnicity by einde
}


//custom method for input Field
public String getUsernameInputFieldValue() {
usernameInputField.getAttribute("value");
}

public void enterUsernameInputField(String param) {
usernameInputField.sendKeys(param);
}


//custom method for button
public void clickSubmitButton() throws PageValidationException {
submitButton.click();
}

//...
}

这里我有一个 SomePage 类,它是一个由字段 (WebElement) 组成的页面对象(使用页面工厂模式)。

根据字段名的后缀我需要创建一些方法。如果后缀是 *Select,该字段将具有许多方法,这些方法对于所有 select field 都是相似的。如果后缀是 *Button,它将具有 button field 所需的方法。

我能否根据字段名称后缀生成自定义方法。

NOTE: IntelliJ Supports the modification of custom Getters and Setters but it doesn't allow multiple methods per field. It restricts to 1 Getter and 1 Setter per field. Which is not always the case as per example. I my have multiple setter ( kind of setter) or getter for a field.

最佳答案

可以生成自定义类,但动态类生成是一项相当高级的功能,从您的问题来看,它不必要地复杂。

我认为您应该为这些具有相同功能的对象创建一个新类。您应该阅读面向对象编程,因为这是一个相当简单的概念。也许你的例子很糟糕,但你为什么要为每个按钮重新定义 String 函数?这是一个不同设计的示例:

public class Button {
private String text;

public Button(String text) {
this.text = text;
}

public void setText(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

然后声明一堆你的新类。

class Temp {
Button abcButton = new Button("abc");
Button cdeButton = new Button("cde");

// Instead of setAbcButtonToLower()
abcButton.setText(abcButton.getText().toLowerCase());
// Instead of getAbcButtonIndexOfChar()
abcButton.getText().indexOf(c);
// Instead of getAbcButtonSize()
abcButton.getText().length();
}

关于java:如何为变量自动生成自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com