我正在一个新的Android项目(Java)上工作,并创建了一个具有大量变量的对象。由于我计划为所有这些类添加getter和setter,我想知道:在Eclipse中是否有自动生成给定类中的getter和setter的快捷方式?


当前回答

在Eclipse中打开类文件 双击类名或突出显示它 然后导航到源->插入代码 单击Getter和Setter

它会打开一个弹出窗口来选择要为其生成getter/setter方法的字段。选择字段并点击“生成”按钮。

其他回答

在Mac OS上是Alt+Cmd+S然后选择“…”getter和setter "

按下Alt + Shift + S + R……然后只选择你必须生成getter或setter或两者的所有字段

Eclipse >源>生成getter和setter

对于pojo使用Project Lombok或更好的Kotlin。

(同时,将Kotlin添加到你的简历中;))

这样的:

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
}

会变成这样:

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

或者使用Kotlin的“数据”类。你最终会写更少的代码行。

是的。右键单击代码,你会看到一个菜单弹出;在Source, Generate Getters and Setters旁边你可以看到快捷键,在我的系统中是Alt+Shift+S和R。

类似地,您可以导航到该主菜单中的其他子菜单,通过键入适当的快捷方式,您可以直接进入子菜单而不是主上下文菜单,然后可以从菜单中选择或从列表中选择另一个字母。