有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android领域子类实例方法

我已经读了很多关于Realm的书,它似乎是一个非常简洁的工具,我很想尝试一下;然而,我在几个不同的地方读到,Android版本不支持RealmObject子类的非静态方法

这在their documentation中并不完全清楚。这意味着不支持非静态方法,但在它们的FAQ部分,在下,为什么我需要为所有字段设置getter和setter,它们使用公共的、非静态的方法

此外,它在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.

所以我有点困惑。我明白我不能有定制的getter/setter;我不喜欢,但这不是一个交易破坏者。但不能拥有任何非静态实例方法则是另一回事

那是哪一个呢?我的RealmObject子类中是否可以有非静态实例方法

谢谢


共 (1) 个答案

  1. # 1 楼答案

    你是对的,你不能有自定义的getter和setter

    允许的内容:

    • 只有私有实例字段
    • 只有默认的getter和setter方法
    • 静态字段,包括公共字段和私有字段
    • 静态方法
    • 实现没有方法的接口

    什么是不允许的:

    • 自定义getter和setter
    • 从非现实对象扩展而来
    • equals()上覆盖toString()
    • 其他自定义非静态方法

    但是they are working on it

    明确地说,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.

    Source