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

时间:2015-12-16 15:30:25

标签: java intellij-idea getter-setter

我的情况是我想为我的字段定义各种行为。

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所需的方法。

我可以根据suffix of field names生成自定义方法。

  

注意:IntelliJ支持修改自定义Getter和Setter,但不允许每个字段使用多个方法。它限制为每个字段1个Getter和1个Setter。根据示例并非总是如此。我有一个多种二传手(一种二传手)或一个场上的吸气剂。

3 个答案:

答案 0 :(得分:0)

在IntelliJ IDEA中生成Getters和Setter:

  1. 在主菜单上,选择“代码”。 或者,右键单击编辑器并在上下文菜单中选择“生成”,或使用“Alt + Insert键盘快捷键”。
  2. 在编辑器中显示的弹出列表中,选择以下选项之一: 消气 用于获取所选字段的当前值的访问器方法。 二传手 用于将指定值设置到所选字段的Mutator方法。 Getter和Setter 选定字段的两种方法。
  3. 在“选择要生成的内容和设置器的字段”对话框中,选择所需的字段。 您可以通过单击browseButton并访问Getter / Setter Templates对话框来添加自定义setter或getter。 如果字段的getter和setter已存在,则此字段不包含在列表中。
  4. 单击“确定” 有关详情,请转至link

答案 1 :(得分:0)

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

我认为您应该为具有相同功能的这些对象创建一个新类。您应该阅读面向对象编程,因为这是一个相当简单的概念。也许您的示例很差,但为什么要为每个按钮重新定义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();
}

答案 2 :(得分:-4)

您无法自动生成自定义方法。对于自定义方法,您必须定义它们,这就是它被称为自定义的原因。 你能得到的最好的只是基本的getter和setter方法,我想你已经有了这些 要么 你可以这样做 class Button {   字符串按钮 }

class buttonProperties {

int buttonLength; int buttonSize;

public int getbuttonSize(){

return buttonSize;

} 。 。

}