变量名称的首字母从大写变为小写 [英] First letter of variable's name is changing from uppercase to lowercase

查看:437
本文介绍了变量名称的首字母从大写变为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个iOS应用,该应用将数据存储到Firebase实时数据库中,如下所示:

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

现在,我开始使用Kotlin进行Android开发,并且数据以值名称的首字母小写存储,如下所示:

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)

这是我的代码:

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{

            }

    }

我希望值的名称与在类中的命名方式完全相同(就像iOS应用程序将其保存一样).

I expect the names of the values to be exactly how they are named in the class (like the iOS app saves them).

推荐答案

Kotlin在JVM(Java虚拟机)上运行,因此为了能够运行.kt文件,首先应将其编译为一个Java文件.因此,当使用如下所示的类时:

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 = "")

创建相应的Java文件,并根据 Java命名约定重新获得变量:

The corresponding Java file is created and according to the Java Naming Conventions regading variables:

它应该是小写字母,例如javalang.

这意味着无论您选择字段名称以大写字母还是小写字母开头,所有字段都将转换为以小写字母开头.这就是为什么数据库中的属性会这样显示.该约定不仅在Java/Kotlin中可用,而且在其他编程语言中也可用,因此我建议您在项目中使用此建议.

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.

关于Kotlin,请记住,最后,所有内容都将转换为字节码,以便可以在JVM上运行.

Regarding Kotlin, remember that at the end, everything is converted to byte code so it can work on JVM.

这篇关于变量名称的首字母从大写变为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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