概述
I created an iOS app that stores the data to Firebase realtime database like this:
users
- ID
-- Email: "test@domain.com"
-- UsersName: "test"
-- Level: 1
-- XP: 0
Now I started with Android development using Kotlin and the data is stored with the first letter of the values name lowercased like this:
users
- ID
-- email: "test@domain.com"
-- usersName: "test" (No mistake, the N is uppercased)
-- level: 1
-- xp: 0 (No mistake, the P is lowercase)
This is my code:
data class User(var UsersName: String = "",
var XP: Int = 0,
var Level: Int = 1,
var Email: String = "")
private fun saveUserToFirebaseDatabase(){
val uid = FirebaseAuth.getInstance().uid ?: ""
val ref = FirebaseDatabase.getInstance().getReference("users/$uid")
val mDatabase = FirebaseDatabase.getInstance().getReference();
val username = usernameSignUp.text.toString()
val user = User(username, 0, 1, SignUpEmailtext.text.toString())
print(user)
mDatabase.child("users").child(uid).setValue(user)
.addOnSuccessListener {
}
.addOnFailureListener{
}
}
I expect the names of the values to be exactly how they are named in the class (like the iOS app saves them).
解决方案
Kotlin works on JVM (Java Virtual Machine), so in order to be able to run a .kt file, first of all it should be compiled into a Java file. So when using a class that looks like:
data class User(var UsersName: String = "",
var XP: Int = 0,
var Level: Int = 1,
var Email: String = "")
The corresponding Java file is created and according to the Java Naming Conventions regading variables:
It should be a lowercase letter such as java, lang.
This means that doesn't matter if you choose the name of your fields to start either with an uppercase or a lowercase, all fiels are converted to start with a lowercase. That's why the properties in your database are present like that. Not only in Java/Kotlin is this convention available but also in other programming languages, so I recommend you to use this recommendation in your projects.
Regarding Kotlin, remember that at the end, everything is converted to byte code so it can work on JVM.
最后
以上就是鲤鱼黄豆为你收集整理的java变量名首字母小写,变量名称的首字母从大写变为小写的全部内容,希望文章能够帮你解决java变量名首字母小写,变量名称的首字母从大写变为小写所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复