简体   繁体   English

有没有办法在 Eclipse 中自动生成 getter 和 setter?

[英]Is there a way to automatically generate getters and setters in Eclipse?

I am working on a new Android project ( Java ), and created an Object with a large number of variables.我正在开发一个新的Android项目 ( Java ),并创建了一个包含大量变量的对象。 Since I am planning to add getters and setters for all of them, I was wondering: is there a shortcut in Eclipse for automatically generating the getters and setters in a given class?由于我打算为所有这些添加 getter 和 setter,我想知道: Eclipse是否有快捷方式可以在给定的类中自动生成 getter 和 setter?

Bring up the context menu (ie right click) in the source code window of the desired class.在所需类的源代码窗口中调出上下文菜单(即右键单击)。 Then select the Source submenu;然后选择Source子菜单; from that menu selecting Generate Getters and Setters... will cause a wizard window to appear.从该菜单中选择Generate Getters and Setters...将出现一个向导窗口。

Source -> Generate Getters and Setters...

Select the variables you wish to create getters and setters for and click OK .选择您希望为其创建 getter 和 setter 的变量,然后单击OK

In Eclipse Juno, by default, ALT+SHIFT+S,R opens the getter/setter dialog box.在 Eclipse Juno 中,默认情况下, ALT+SHIFT+S,R打开 getter/setter 对话框。 Note you have to press all 4 keys.请注意,您必须按所有 4 个键。

Right click -> Source -> Generate setters and getters

但为了更方便,我总是将它映射到ALT + SHIFT + GWindows -> Preferences -> General -> Keys

All the other answers are just focus on the IDE level, these are not the most effective and elegant way to generate getters and setters.所有其他答案都只关注 IDE 级别,这些并不是生成 getter 和 setter 的最有效和最优雅的方式。 If you have tens of attributes, the relevant getters and setters methods will make your class code very verbose.如果你有几十个属性,相关的 getter 和 setter 方法会让你的类代码非常冗长。

The best way I ever used to generate getters and setters automatically is using project lombok annotations in your java project , lombok.jar will generate getter and setter method when you compile java code.我曾经用来自动生成 getter 和 setter 的最好方法是在您的 java 项目中使用项目 lombok 注释,lombok.jar 将在您编译 java 代码时生成 getter 和 setter 方法。

You just focus on class attributes/variables naming and definition, lombok will do the rest.您只需关注类属性/变量的命名和定义,lombok 将完成其余的工作。 This is easy to maintain your code.这很容易维护您的代码。

For example, if you want to add getter and setter method for age variable, you just add two lombok annotations:例如,如果要为age变量添加getter和setter方法,只需添加两个lombok注解:

@Getter @Setter 
public int age = 10;

This is equal to code like that:这相当于这样的代码:

private int age = 10;
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

You can find more details about lombok here: Project Lombok您可以在此处找到有关 lombok 的更多详细信息: Project Lombok

Ways to Generate Getters & Setters -生成 Getter 和 Setter 的方法 -

1) Press Alt + Shift + S , then R 1)Alt + Shift + S ,然后按R
2) Right click -> Source -> Generate Getters & Setters 2)右击-> Source -> Generate Getters & Setters
3) Go to Source menu -> Generate Getters & Setters 3) 转到Source 菜单-> Generate Getters & Setters
4) Go to Windows menu -> Preferences -> General -> Keys (Write Generate Getters & Setters on text field) 4) 转到Windows 菜单-> Preferences -> General -> Keys (Write Generate Getters & Setters on text field)
5) Click on error bulb of the field -> create getters & setters ... 5)单击该字段的错误灯泡-> 创建 getter 和 setter ...
6) Press Ctrl + 3 and write getters & setters on text field then select option Generate Getters & Setters 6)Ctrl + 3并在文本字段上写入 getters & setters 然后选择选项 Generate Getters & Setters

if Mac OS press Alt+cmd+S then select Getters & Setters如果Mac OS按 Alt+cmd+S 然后选择 Getters & Setters

Right click-> generate getters and setters does the job well but if you want to create a keyboard shortcut in eclipse in windows, you can follow the following steps:右键单击-> 生成 getter 和 setter 可以很好地完成这项工作,但是如果您想在 windows 中的 eclipse 中创建键盘快捷键,您可以按照以下步骤操作:

  1. Go to Window > Preferences转到窗口 > 首选项
  2. Go to General > Keys转到“常规”>“键”
  3. List for "Quick Assist - Create getter/setter for field" “快速辅助 - 为字段创建 getter/setter”列表
  4. In the "Binding" textfield below, hold the desired keys (in my case, I use ALT + SHIFT + G)在下面的“绑定”文本字段中,按住所需的键(在我的情况下,我使用 ALT + SHIFT + G)
  5. Hit Apply and Ok点击应用并确定
  6. Now in your Java editor, select the field you want to create getter/setter methods for and press the shortcut you setup in Step 4. Hit ok in this window to create the methods.现在在您的 Java 编辑器中,选择要为其创建 getter/setter 方法的字段,然后按您在步骤 4 中设置的快捷方式。在此窗口中点击确定以创建方法。

Hope this helps!希望这可以帮助!

Sure.当然。

Use Generate Getters and Setters from the Source menu or the context menu on a selected field or type, or a text selection in a type to open the dialog.使用“源”菜单或所选字段或类型的上下文菜单中的“生成 Getter 和 Setter”,或类型中的文本选择来打开对话框。 The Generate Getters and Setters dialog shows getters and setters for all fields of the selected type.生成 Getter 和 Setter 对话框显示所选类型的所有字段的 getter 和 setter。 The methods are grouped by the type's fields.这些方法按类型的字段分组。

Take a look at the help documentation for more information.查看帮助文档以获取更多信息。

Mac OS 上,它是Alt+Cmd+S然后选择“...Getters and Setters”

Yes.是的。 Right-click on code and you see a menu pop up;右键单击代码,您会看到一个弹出菜单; there "Source", "Generate Getters and Setters" and next to it you can see the shortcut, which is Alt+Shift+S and R on my system.有“源”,“生成getter和setter”,并在它旁边,你可以看到快捷键,其键是Alt + Shift + S和R我的系统上。

Similarly you can navigate to other submenus in that main menu, by typing the appropriate shortcut you go straight the submenu instead of main context menu, and can then either pick from menu or type another letter to pick from the list.同样,您可以导航到该主菜单中的其他子菜单,通过键入适当的快捷方式,您可以直接进入子菜单而不是主上下文菜单,然后可以从菜单中选择或键入另一个字母以从列表中选择。

I prefer to create the private field first我更喜欢先创建私有字段

private String field;

Eclipse will auto highlight the variable, by positioning cursor over your new variable, press Ctrl + 1. It will then give you the menu to Create getter and setter. Eclipse 将自动突出显示变量,通过将光标定位在新变量上,按 Ctrl + 1。然后它会为您提供创建 getter 和 setter 的菜单。

I press Ctrl + 1 because it is a bit more intelligent about what I think you want next.我按 Ctrl + 1 是因为它对我认为您接下来想要的内容更加智能。

  • For All variable ALT+SHIFT+S Then R and for select all Press ALT+A对于所有变量ALT+SHIFT+S 然后 R 并选择所有 按 ALT+A

  • For Single variable Point cursor on the variable then press CTRL+1 and go for the second option from suggestions对于单个变量变量上点光标,然后按 CTRL+1 并从建议中选择第二个选项

截屏

按 Alt+Shift+S+R... 然后只选择您必须生成 Getter 或 Setter 或两者的所有字段

There is an open source jar available know as Lombok , you just add jar and then annotate your POJO with @Getter & @Setter it will create getters and setters automatically.有一个名为Lombok 的开源 jar,您只需添加 jar,然后使用@Getter 和 @Setter注释您的 POJO,它将自动创建 getter 和 setter。

Apart from this we can use other features like @ToString , @EqualsAndHashCode and pretty other cool stuff which removes vanilla code from your application除此之外,我们还可以使用其他功能,例如@ToString@EqualsAndHashCode和其他一些很酷的东西,它们可以从您的应用程序中删除普通代码

Eclipse > 源代码 > 生成 Getter 和 Setter

**In Eclipse Ide **在 Eclipse IDE 中

for generating both setters and getters -> alt+shift+s+r then Alt A then click on ok;用于生成 setter 和 getter -> alt+shift+s+r 然后 Alt A 然后单击确定;

for generating only getters ->alt+shift+s+r then press g then click on ok button;只生成 getter ->alt+shift+s+r 然后按 g 然后点击 ok 按钮;

for generating only setters ->alt+shift+s+r then press l then click on ok button;**只生成 setter ->alt+shift+s+r 然后按 l 然后点击 ok 按钮;**

右键单击要为其生成 getter 和 setter 的属性,然后选择

Source -> Generate Getters and Setters...

1) Go to Windows->Preferences->General->Keys 1)转到Windows->首选项->常规->键

2) Select the command " Generate Getters and Setters " 2) 选择commandGenerate Getters and Setters

3) In the Binding , press the shortcut to like to use (like Alt+Shift+G ) 3) 在Binding喜欢使用的快捷键(如Alt+Shift+G

4) Click apply and you are good to go 4)点击申请,你就可以开始了

  1. Open the class file in Eclipse在 Eclipse 中打开类文件
  2. Double click on the class name or highlight it双击班级名称或突出显示它
  3. Then navigate to Source -> Insert Code然后导航到 Source -> Insert Code
  4. Click on Getter and Setter单击 Getter 和 Setter

It opens a popup to select the fields for which getter/setter methods to be generated.它会打开一个弹出窗口来选择要为其生成 getter/setter 方法的字段。 Select the fields and click on "Generate" button.选择字段并单击“生成”按钮。 在此处输入图片说明 在此处输入图片说明

Use Project Lombok or better Kotlin for your Pojos.为您的 Pojo 使用 Project Lombok 或更好的 Kotlin。

(Also, to add Kotlin to your resume ;) ) (另外,将 Kotlin 添加到您的简历中 ;))

This :这 :

public class BaseVO {
    protected Long id;

    @Override
    public boolean equals(Object obj) {
        if (obj == null || id == null)
            return false;

        if (obj instanceof BaseVO)
            return ((BaseVO) obj).getId().equals(id);

        return false; 
    }

    @Override
    public int hashCode() {
        return id == null ? null : id.hashCode();
    }
    // getter setter here
}

public class Subclass extends BaseVO {
    protected String name;
    protected String category;
    // getter setter here
}

would become this :会变成这样:

open class BaseVO(var id: Long? = null) {

    override fun hashCode(): Int {
        if (id != null)
            return id.hashCode()

        return super.hashCode()
    }

    override fun equals(other: Any?): Boolean {
        if (id == null || other == null || other !is BaseVO)
            return false

        return id.hashCode() == other.id?.hashCode()
    }
}

@Suppress("unused")
class Subclass(
        var name: String? = null,
        var category: String? = null
) : BaseVO()

Or use Kotlin's "data" classes.或者使用 Kotlin 的“数据”类。 You end up writing even fewer lines of code.您最终会编写更少的代码行。

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

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