Getter不与任何字段相关联 [英] Getter is not associated to any field

查看:116
本文介绍了Getter不与任何字段相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码,我收到编译器错误 Getter getLength_inch与任何字段无关。 getLength_inch只是一种实用方法......

I am getting a compiler error "Getter getLength_inch is not associated to any field" for the following code. getLength_inch is just a utility method...

import io.realm.RealmObject;  
public class Measurement extends RealmObject {
    private float length_mm;

    public void setLength_mm(float c){length_mm = c;}
    public float getLength_mm() { return length_mm;}

    public float getLength_inch() { return length_mm * 0.0394f;}  
}

似乎任何RealmObject的后代都无法实现任何东西除了与其领域相关的东西。这是正确的还是有一些方法来注释这个方法,以便你的处理器忽略它?

It seems that any descendant of RealmObject cannot implement anything else than what is pertinent to the its fields. Is this correct or is there some way to annotate this method so that your processor ignores it?

谢谢

推荐答案

作为一种解决当前不允许使用@Ignore on方法的realm.io限制的工作,我使用内部类并将其他功能作为扩展添加到其中。通过这种方式,主要课程仍然保持最低限度,并且领域不会抱怨我还有其他功能并保持某种形式的优雅。

As a work around to the limitation of realm.io not currently allowing the @Ignore on methods, I used inner classes and put the additional functionality in it as an "extension". This way the main class remain minimal and realm does not complain yet I get to have my additional functionality and keep some form of elegance.

public class Measurement extends RealmObject {
  private float length_mm;
  @Ignore
  private Extension ex;

  public void setLength_mm(float c){length_mm = c;}
  public float getLength_mm() { return length_mm;}

  public class Extension {
     public float getLength_inch() { return length_mm * 0.0394f;}  
  }
  public Extension getEx(){
    if (ex == null) ex = new Extension();
    return ex;
  }

然后我使用getEx()来访问扩展方法。

Then I use getEx() to get access to the "extension methods".

float l = myMeasurement.getEx().getLength_inch();

这篇关于Getter不与任何字段相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆