java - 什么时候/为什么我应该在方法上使用 Firebase @Exclude 注释

标签 java android firebase firebase-realtime-database

来自 here我知道我们应该使用这个注解来排除字段被保存在 firebase 上。我知道的就这么多

但来自here我们可以看到这个注解是为了它也可以用在方法上!正如在几个 examples 上看到的那样但是我为什么要@Exclude 一个方法呢?如果我不使用 @Exclude 方法会怎样?因为保存的只是字段,所以我不知道什么时候/为什么要在方法上使用这个注释。

编辑

正如你们所问,这是一个示例,发布在上面的链接中,在 firebase 项目中,firebase 团队在既不是 getter 也不是 setter 的方法中使用@Exclude

@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();
    result.put("id", id);
    result.put("fullName",fullName);
    result.put("birthDate",birthDate);
    result.put("birthYear", birthYear);
    result.put("height",height);
    result.put("aboutMe",aboutMe);
    result.put("userLocation",userLocation);
    result.put("jobPosition",jobPosition);
    result.put("companyName",companyName);
    result.put("companyLocation",companyLocation);
    result.put("jobStartDate",jobStartDate);
    result.put("homeEmail",homeEmail);
    result.put("homePhone",homePhone);
    result.put("workEmail",workEmail);
    result.put("workPhone",workPhone);
    result.put("facebookName",facebookName);
    result.put("facebookLink",facebookLink);
    result.put("instaName",instaName);
    result.put("instaLink",instaLink);
    return result;
}

最佳答案

如果字段是公共(public)的,您可以在字段上使用@Exclude,并且可以直接访问它们的值。例如:

public class Pojo {
    public String name;
    @Exclude
    public int age;
}

当底层字段是私有(private)的并且所有访问都必须通过这些访问器时,您可以在 getter 和 setter 方法上使用 @Exclude。例如:

public class Pojo {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    @Exclude
    public int getAge() {
        return age;
    }
}

Firebase SDK 可以发现和使用这两种类型。

关于java - 什么时候/为什么我应该在方法上使用 Firebase @Exclude 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51774621/

相关文章:

firebase - 将 Firebase 实时数据库迁移到 Firestore

firebase - 恢复已删除的 Firebase 集合

java - 如何在Java中实现嵌套集合的迭代器?

java - Android - 将现有应用程序作为库项目添加到另一个应用程序

java - 在 Java 中可用的基于 Web 的开源 ERP 系统?

android - 在代理后面的 IDEA 中运行 Robolectric

java - 测量圈复杂度

android - 深度链接不会在 Android 中自动打开

android - 账户管理器 - "Permission is only granted to system apps"

android - 如何查询 Firebase Firestore 引用数据类型?