简体   繁体   English

是否可以扩展其他类并实现RealmModel?

[英]Is it possible to extend some other class and implement RealmModel?

I'm trying to extend my model from a BaseModel and then implement RealmModel . 我正在尝试从BaseModel扩展我的模型,然后实现RealmModel

@RealmClass
public class User extend BaseModel implements RealmModel

But it is throwing an error: 但这是一个错误:

error: Realm model classes must either extend RealmObject or implement RealmModel to be considered a valid model class

Please help. 请帮忙。

As mentioned in Realm's GitHub repository, inheritance and polymorphism are not implemented in Realm just yet. 正如Realm的GitHub存储库中所提到的,尚未在Realm中实现继承和多态。

As one of the contributors described it : 正如其中一位贡献者所描述的那样

Realm currently is using byte code manipulation to replace field accessors and using annotation processor to create proxy classes. Realm目前正在使用字节代码操作来替换字段访问器并使用注释处理器来创建代理类。 Enabling RealmObject to extend from the other class/abstract class will increase a bit complexity of Realm. 使RealmObject从其他类/抽象类扩展将增加Realm的一点复杂性。 So it is not implemented right now. 所以它现在还没有实现。 But I agree it is something we should support. 但我同意这是我们应该支持的。

You can read more here . 你可以在这里阅读更多。

I guess that for the time being, you will have to think of another way to write your classes. 我想现在,你将不得不考虑另一种编写课程的方法。

It is very unfortunate, but RealmModel exists so that you don't need to inherit properties and methods from RealmObject . 这是非常不幸的,但RealmModel存在,因此您不需要从RealmObject继承属性和方法。

I tried extending a base class that only contained fields annotated with @Ignore , but even that is not supported at the moment . 我尝试扩展一个只包含用@Ignore注释的字段的基类,但即使这样也不支持

Only way to "share fields" is to share the accessors through interface. “共享字段”的唯一方法是通过接口共享访问者。

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

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