简体   繁体   English

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

[英]java: how to auto-generate a custom method for a variable

I've a situation where i want to define variety of behaviour for my fields. 我有一种情况,我想为我的领域定义各种行为。

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

    //...
}

Here I have a class SomePage which is a Page Object (Using Page Factory Pattern) which consist of fields ( WebElement ). 这里我有一个SomePage类,它是一个由对象组成的页面对象(使用页面工厂模式)( WebElement )。

Based on the suffix of field names I need to create number of method. 基于字段名称的后缀我需要创建多个方法。 If suffix is *Select the field will have number of methods which can be Similar for all select field . 如果后缀为*Select该字段将具有多个方法,对于所有select field可以是相似的。 If suffix is *Button it will have a method's that are needed for button field . 如果后缀为*Button则它将具有button field所需的方法。

Can I generate the Custom Method based upon the suffix of field names . 我可以根据suffix of field namessuffix of field names生成自定义方法。

NOTE: IntelliJ Supports the modification of custom Getters and Setters but it doesn't allow multiple methods per field. 注意:IntelliJ支持修改自定义Getter和Setter,但它不允许每个字段使用多个方法。 It restricts to 1 Getter and 1 Setter per field. 它限制为每个字段1个Getter和1个Setter。 Which is not always the case as per example. 根据示例并非总是如此。 I my have multiple setter ( kind of setter) or getter for a field. 我有一个场的多个二传手(一种二传手)或吸气剂。

Generating Getters and Setters in IntelliJ IDEA : 在IntelliJ IDEA中生成getter和setter:

  1. On the main menu, choose Code. 在主菜单上,选择“代码”。 Alternatively, right-click the editor and choose Generate on the context menu, or use Alt+Insert keyboard shortcut. 或者,右键单击编辑器并在上下文菜单中选择“生成”,或使用“Alt + Insert”键盘快捷键。
  2. In the pop-up list that is displayed in the editor, select one of the following options: Getter Accessor method for getting the current value of the selected fields. 在编辑器中显示的弹出列表中,选择以下选项之一:Getter Accessor方法,用于获取所选字段的当前值。 Setter Mutator method for setting specified values to the selected fields. Setter Mutator方法,用于将指定值设置为所选字段。 Getter and Setter Both methods for the selected fields. Getter和Setter所选字段的两种方法。
  3. In the Choose Fields to Generate Getters and Setters dialog box, select the desired fields. 在“选择要生成的getters和Setter的字段”对话框中,选择所需的字段。 You can add a custom setter or getter by clicking browseButton and accessing Getter/Setter Templates dialog. 您可以通过单击browseButton并访问Getter / Setter Templates对话框来添加自定义setter或getter。 If getters and setters for a field already exist, this field is not included in the list. 如果字段的getter和setter已存在,则此字段不包含在列表中。
  4. Click OK. 单击确定。
    For more info go to link 有关更多信息,请转到链接

It is possible to generate custom classes, but dynamic class generation is a fairly advanced feature, and from the sounds of your problem it's unnecessarily complex. 可以生成自定义类,但动态类生成是一个相当高级的功能,从您的问题的声音,它是不必要的复杂。

I think you should create a new class for these objects with the same functionality. 我认为你应该为具有相同功能的这些对象创建一个新类。 You should read about Object-Oriented Programming because this is a fairly simple concept. 您应该阅读面向对象编程,因为这是一个相当简单的概念。 Maybe your example is poor, but why are you redefining String functions for each button? 也许您的示例很差,但为什么要为每个按钮重新定义String函数? Here's an example of a different design: 这是一个不同设计的例子:

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

Then just declare a bunch of your new class. 然后宣布一堆你的新班级。

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

You cant generate custom methods automatically . 您无法自动生成自定义方法。 For custom methods you have to define them that's why it is called custom . 对于自定义方法,您必须定义它们,这就是它被称为自定义的原因。 best you can get are only basic getter and setter methods, I think you already got those 你能得到的最好的只是基本的getter和setter方法,我想你已经有了
or you can do it like this class Button{ string button } 或者你可以像这样的类按钮{string button}

class buttonProperties{ class buttonProperties {

int buttonLength; int buttonLength; int buttonSize; int buttonSize;

public int getbuttonSize(){ public int getbuttonSize(){

return buttonSize;

} . }。 . .

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM