Android 领域子类实例方法
Android Realm Subclass Instance Methods
我已经阅读了很多关于 Realm 的文章,它似乎是一个非常简洁的工具,我很想尝试一下;但是,我在几个不同的地方读到 Android 版本不支持 RealmObject 子类的非静态方法。
这在 their documentation. This implies that non-static methods aren't supported, but in their FAQ 部分中不是很清楚,在 为什么我的所有字段都需要 getter 和 setter?,他们使用 public, 非静态方法。
此外,this 文章中非常清楚:
You will get compilation errors for using ANY other methods in the model class. Think about this for a moment ...
Yeah.. you can not have toString(), Static methods, not even other behavior methods in your model classes.
所以我有点困惑。我知道我不能自定义 getters/setters;我不喜欢它,但这不是一个交易破坏者。但是不能有任何非静态实例方法是另一回事。
那是哪一个?我可以或不可以在我的 RealmObject 子类中使用非静态实例方法吗?
谢谢。
你是对的,你不能有自定义的 getters 和 setters。
允许的内容:
- 仅私有实例字段。
- 只有默认的 getter 和 setter 方法。
- 静态字段,public 和私有字段。
- 静态方法。
- 不使用方法实现接口。
什么是不允许的:
- 自定义 getters 和 setters
- 从非 RealmObject 扩展
- 在
equals()
上覆盖 toString()
- 其他自定义非静态方法
明确一点,RTFM:
Be aware that the getters and setters will be overridden by the generated proxy class used in the back by RealmObjects, so any custom logic you add to the getters & setters will not actually be executed.
Limitations
Due to how the proxy classes override getters and setters in the model classes there are some restrictions to what is allowed in a model class.
Due to how the proxy classes override getters and setters in the model
classes there are some restrictions to what is allowed in a model
class:
Only private instance fields.
Only default getter and setter methods.
Static fields, both public and private.
Static methods.
Implementing interfaces with no methods.
This means that it is currently not possible to extend anything else
than RealmObject or to override methods like toString() or equals().
Also it is only possible to implement interfaces.
您的 RealmObject 子class应该是存储数据的 POJO。
所以他们可以有私有字段,public getters/setters,和静态 methods/classes/enums/interfaces。就是这样。
如果您还需要其他任何东西,您应该将其移至另一个 class,该 class 具有将您的 realmObject
作为参数的方法。
我已经阅读了很多关于 Realm 的文章,它似乎是一个非常简洁的工具,我很想尝试一下;但是,我在几个不同的地方读到 Android 版本不支持 RealmObject 子类的非静态方法。
这在 their documentation. This implies that non-static methods aren't supported, but in their FAQ 部分中不是很清楚,在 为什么我的所有字段都需要 getter 和 setter?,他们使用 public, 非静态方法。
此外,this 文章中非常清楚:
You will get compilation errors for using ANY other methods in the model class. Think about this for a moment ...
Yeah.. you can not have toString(), Static methods, not even other behavior methods in your model classes.
所以我有点困惑。我知道我不能自定义 getters/setters;我不喜欢它,但这不是一个交易破坏者。但是不能有任何非静态实例方法是另一回事。
那是哪一个?我可以或不可以在我的 RealmObject 子类中使用非静态实例方法吗?
谢谢。
你是对的,你不能有自定义的 getters 和 setters。
允许的内容:
- 仅私有实例字段。
- 只有默认的 getter 和 setter 方法。
- 静态字段,public 和私有字段。
- 静态方法。
- 不使用方法实现接口。
什么是不允许的:
- 自定义 getters 和 setters
- 从非 RealmObject 扩展
- 在
equals()
上覆盖 - 其他自定义非静态方法
toString()
明确一点,RTFM:
Be aware that the getters and setters will be overridden by the generated proxy class used in the back by RealmObjects, so any custom logic you add to the getters & setters will not actually be executed.
Limitations
Due to how the proxy classes override getters and setters in the model classes there are some restrictions to what is allowed in a model class.Due to how the proxy classes override getters and setters in the model classes there are some restrictions to what is allowed in a model class:
Only private instance fields. Only default getter and setter methods. Static fields, both public and private. Static methods. Implementing interfaces with no methods.
This means that it is currently not possible to extend anything else than RealmObject or to override methods like toString() or equals(). Also it is only possible to implement interfaces.
您的 RealmObject 子class应该是存储数据的 POJO。
所以他们可以有私有字段,public getters/setters,和静态 methods/classes/enums/interfaces。就是这样。
如果您还需要其他任何东西,您应该将其移至另一个 class,该 class 具有将您的 realmObject
作为参数的方法。