java - 使用一个类的多个constrictor在firebase中存储数据

标签 java android firebase firebase-realtime-database constructor

我有一个类 User 具有属性 name,age,nationality,eyeColor 我有三个构造函数。

  1. Firebase 所需的空构造函数,
  2. 用户(姓名,国籍),
  3. 用户(姓名、国籍、年龄、眼睛颜色)。

每当我尝试使用构造函数 2 在 Firebase 中保存数据时,我发现数据实际上是使用构造函数 3 保存的,ageeyeColor0null

如何在不为构造函数 3 创建新类的情况下仅使用构造函数 2 的参数保存数据?

最佳答案

这是一个有趣的问题。我以前没有遇到过这种情况。然而,看完documentation ,它说,

First, create a database reference to your user data. Then use set() / setValue() to save a user object to the database with the user's username, full name, and birthday. You can pass set a string, number, boolean, null, array or any JSON object. Passing null will remove the data at the specified location.

我建议您像这样构建第二个构造函数。

public User(String name, String nationality) {
    this.name = name;  
    this.nationality = nationality;
    this.age = null;
    this.eyeColor = null;
}

我希望这不会将添加的信息保存在用户 key 中。让我知道是否有帮助!

关于java - 使用一个类的多个constrictor在firebase中存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50914215/

相关文章:

java - 在带有属性的 XML 中使用 org.apache.commons.digester.Digester

android - 防止第二个 ajax 调用等到第一个 ajax 调用完成

android - 使用 Intel XDK 和 Cordova CLI : What is better? 的经验

Firebase HTTP 云功能 - 500 服务器错误,请重试

javascript - 将文件存储在 Firebase 存储中

调用 main 函数方法时发生 Java 引用错误

java - 使用 FloatMath 或 Math 和类型转换?

java - 使用连续字符键的通用二分搜索失败

android - 单击TDateEdit(FMX Control)导致Android应用程序崩溃

java - Firebase Crashlytics | Firebase 崩溃捕获的异常是否显示?