简体   繁体   English

Java注释字段setter和getter如何

[英]Java annotation fields setter and getter how to

What I want to accomplish but don't understand really how it works its 我想要完成但却不明白它是如何运作的

@Table(name = "Categories")
public class Category extends Model { 
    @Column(name = "Name")
    private String name;
}

With this code I want to annotation generate their respective setter and getter so I can use like 有了这段代码,我想要注释生成各自的setter和getter,这样我就可以使用了

Category category = new Category();
category.setName("My Name");
category.save();

Whey I need setter and getter and not access/edit the value directly? 乳清我需要setter和getter而不是直接访问/编辑值? Because some values has different treatment, like relations, and because I want to have fields that don't want to be edited. 因为某些值具有不同的处理方式,例如关系,并且因为我想要具有不想编辑的字段。 And I'm too lazy to do manually all the time this, with every model, also save a lot of work later to just put an annotation and set their field 而且我懒得一直手动做这个,每个模型,以后也节省了很多工作只是放一个注释并设置他们的字段

My inspiration to try this was Android Annotations seems a solid and cool library, I know maybe its too advanced but my goal with this experiment its to have a library like that but focused on models like active record or another orm. 我尝试这个的灵感是Android Annotations似乎是一个坚实而酷的库,我知道它可能太先进但我的目标是这个实验它有一个像这样的库,但专注于像活动记录或其他orm的模型。

Tuts, tips, advices, books are welcome. 欢迎提供Tuts,提示,建议和书籍。

Regards 问候

Edit 2013-10-25 编辑2013-10-25

My goal is to build a library capable to do this, because I'm too curious and want to learn how internally work, so I'll be able to power my framework with this feature, as jet just are small utilities but in the future I hope it save me a lot of work, you can see at github WSD Android 我的目标是建立一个能够做到这一点的图书馆,因为我太好奇了,想要了解内部的工作方式,所以我将能够用这个功能为我的框架提供支持,因为喷气机只是小型实用程序,但在未来我希望它能为我节省很多工作,你可以在github上看到WSD Android

If you are too lazy to create the setters and getters of your variables why not just let your IDE generate it for you? 如果你懒得创建变量的setter和getter,为什么不让你的IDE为你生成呢?

but if you do really insist, take a look at this plugin 但如果你确实坚持,请看一下这个插件

This simply allows you to do this 这只是允许你这样做

@Table(name = "Categories")
public class Category extends Model { 

    @Setter
    @Getter
    @Column(name = "Name")
    private String name;
}

WARNING. 警告。

the plugin is not well documented, you also need to configure your IDE to actually see it(eg category.getName() ) 插件没有很好的文档记录,你还需要配置你的IDE来实际看到它(例如category.getName()

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

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