问题描述
我想向我的greenDAO实体添加一些自定义代码.我看到那里有一些保护区.但是我不喜欢将生成的类检入到git存储库中的想法.我想为此使用继承.
I want to add some custom code to my greenDAO entities. I saw there is something like protected regions. But I don't like the idea to check in the generated classes to my git repository. I'd like to use inheritance for this.
即我有一个实体User
.因此,我希望greenDAO生成一个名为UserBase
的类.我想通过User
扩展并实现这样的方法:
i.e. I have an entity User
. So I want greenDAO to generate a class called UserBase
. This I want to extend by User
and implement a method like this:
public String getFullName() {
return this.first + " " + this.last;
}
其中first
和last
是托管属性.
但是我不知道如何告诉greenDAO使用类User
而不是生成的实体UserBase
.有什么办法吗?
But I have no idea how to tell greenDAO to use the class User
instead of the generated entity UserBase
. Is there any way to do this?
推荐答案
我找到了一种解决方法:
I found a way how to solve this:
您可以为每个实体输入一个上级:
you can enter a parent for each entity:
Entity user = schema.addEntity("User");
...
user.setSuperclass("UserBase");
因此,您可以将UserBase
实现为抽象类.
So you can implement the UserBase
as an abstract class.
public abstract class UserBase {
public String getFullName() {
return getFirst() + " " + getLast();
}
public abstract int getFirst();
public abstract int getLast();
}
这里的缺点是,必须将生成的getter声明为抽象方法才能访问它们.
The disadvantage here is, that you have to declare the generated getters as abstract methods to access them.
这篇关于向greenDAO实体添加自定义代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!