Kel*_*lli 3 java android kotlin
我正在关注Android 开发者网站上的教程。它在 Java 示例中使用 Kotlin。我在堆栈溢出上找到了这篇关于等效内容的文章,但我不明白答案。
原来的代码是:
// Defines the selection clause
private static final String SELECTION = Data.LOOKUP_KEY + " = ?";
// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
* Defines a variable to contain the selection value. Once you
* have the Cursor from the Contacts table, and you've selected
* the desired row, move the row's LOOKUP_KEY value into this
* variable.
*/
private lateinit var lookupKey: String
Run Code Online (Sandbox Code Playgroud)
我将其重写如下:
// Defines the selection clause
private static final String SELECTION = ContactsContract.Data.LOOKUP_KEY + " = ?";
// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
* Defines a variable to contain the selection value. Once you
* have the Cursor from the Contacts table, and you've selected
* the desired row, move the row's LOOKUP_KEY value into this
* variable.
*/
private String lookupKey;
Run Code Online (Sandbox Code Playgroud)
这个答案是不是太简单了?或者有更复杂的java翻译吗?
lateinit在 Kotlin 中只是处理变量的可空性。由于 Java 没有这样的属性,因此您不能从字面上转换lateinit为 Java。好吧,您可以强制其类型,但无法应用@NonNull/ @Nullable。
Lateinit 和 Lazy 是 Kotlin 值得深入阅读的重要主题。我希望你能继续学习这些知识。
答案是正确的:只需使用private String lookupKey;并且......就是这样。
顺便说一句,lateinit只是在字节码中创建一个if条件,如果为 null 则抛出异常。Java 中没有lateinit,因此需要手动编写代码。这是 Kotlin 相对于 Java 的另一个不错的特性。
| 归档时间: |
|
| 查看次数: |
5252 次 |
| 最近记录: |