gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:54 27 4
gpt4 key购买 nike

我有一个类 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com