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

java:如何为变量自动生成自定义方法,java,intellij-idea,getter-setter,Java,Intellij Idea,Getter Setter,我想为我的领域定义各种行为 class SomePage { //... @FindBy(id = "selectEthinicity") private WebElement ethinicitySelect; @FindBy(id = "someId") private WebElement usernameInputField; @FindBy(id = "buttonSubmit") private WebElement s

我想为我的领域定义各种行为

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
)组成

根据字段名的后缀,我需要创建方法的数量。如果后缀为
*选择
,则该字段将具有许多方法,这些方法对于所有
选择字段
。如果后缀为
*按钮
,则它将具有按钮字段所需的方法

我可以根据字段名的
后缀生成自定义方法吗

注意:IntelliJ支持修改自定义getter和setter,但不允许每个字段使用多个方法。它将每个字段限制为1个Getter和1个Setter。根据例子,情况并非总是如此。我有一个字段的多个setter(setter类型)或getter


在IntelliJ IDEA中生成getter和setter:

  • 在主菜单上,选择“代码”。 或者,右键单击编辑器并在关联菜单上选择“生成”,或者使用Alt+Insert键盘快捷键
  • 在编辑器中显示的弹出列表中,选择以下选项之一: 吸气剂 获取所选字段当前值的访问器方法。 塞特 用于为选定字段设置指定值的Mutator方法。 接二连三 这两种方法都适用于所选字段
  • 在“选择要生成getter和setter的字段”对话框中,选择所需的字段。 您可以通过单击browseButton并访问getter/setter Templates对话框来添加自定义setter或getter。 如果字段的getter和setter已经存在,则该字段不包括在列表中
  • 单击“确定”。
    有关更多信息,请访问

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

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

    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();
    }
    

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

    类按钮属性{

    int按钮长度; int按钮化

    public int getbuttonSize(){

    } . .


    }

    这是大错特错的。Lombok会相应地插入代码。您可能想看看有什么原因不能创建具有已定义函数的类单选/按钮吗?您甚至可以创建一个超类并对其进行扩展,然后在Temp类中只使用单个按钮/单选按钮的getter和setter,然后调用其getter/setter方法。。WebElement中已经有了我需要的功能。但是我使用的是所谓的PageObject模式,所以我需要在页面中为每个元素(变量)使用这些方法。我在那里添加的代码只是一个示例代码。。。我并没有在字符串上执行这些操作。。。。我只是想用一种简单的方式告诉你我需要什么。。请您再次检查我的问题,忽略我的变量作为字符串。。我只是想要一个自定义方法来描述这些变量的行为。我想我的答案仍然适用。创建一个定义了所有常见操作的类。使用超类和子类将是我的方法,而不是为每个对象定制方法。如果每个对象都使用相同的精确方法,那么这些方法应该应用于对象级别。@Compass如果使用继承方法,则必须传递一个要对其执行操作的元素。你的方法很好。。这就是我正在做的,但这不是我想要的解决方案…@Samuel我已经更新了示例,请您再次回顾一下,并让我知道您是否理解我试图实现的目标..Getter and Setter只给出简单的Getter and Setter。。我需要对这些变量做一些其他的事情。。尝试自定义getter和setter。。我不能添加多个getter/setter@AmanTuladhar您的意思是:我不能添加多个getter/setter,因为我有一个变量x。。如果我自动生成getter和setter,我可以有一个getter用于x,一个setter或x。。但是看看我的例子,我有很多方法来处理一个变量。。不仅仅是盖特和塞特。。我需要这些是自动生成的太..我按照你给我的步骤..(链接太)。。其中要定义的部分可能是两个getter/setter
    return buttonSize;