Android 到 Kotlin 的最佳实践转换

huangapple go评论55阅读模式
英文:

Android to Kotlin best practice to convert

问题

首先类别:

第一个类:

开放类 数据库对象 @JvmOverloads 构造函数(
    ctx: 上下文,
    val iD: 整数,
    val 名称: 字符串,
    显示名称: 字符串 = ""
) {
    var 显示名称: 字符串? = null

    初始化 {
        如果 (显示名称 === "") {
            val 浪费字符串键 = ctx.resources.getIdentifier(名称, "string", ctx.packageName)
            如果 (浪费字符串键 != 0) 这个.显示名称 =
                ctx.getString(浪费字符串键) 否则 这个.显示名称 =
                名称.replace("_", " ")
        } 否则 这个.显示名称 = 显示名称
    }

    开放式 函数 获取显示名称(): 字符串? {
        返回 显示名称
    }

    开放式 函数 获取ID(): 整数 {
        返回 iD
    }

    开放式 函数 获取名称(): 字符串? {
        返回 名称
    }
}

第二个类(安卓版本):

公共 类 语言 扩展 数据库对象 {
    私人 字符串 _缩写;
    
    公共 语言(上下文 ctx, 整数 id, 字符串 缩写, 字符串 名称) {
        超级(ctx, id, 名称);
        
        _缩写 = 缩写;
    }
    
    公共 字符串 获取缩写() {
        返回 _缩写;
    }
}

但是如果我将代码复制粘贴到我的类中,IDE 会将我转换成如下的 Kotlin 代码:

开放类 语言(ctx: 上下文?, id: 整数, val 缩写: 字符串, 名称: 字符串?) :
    数据库对象(ctx!!, id, 名称!!)

在另一个类中使用函数 获取缩写() -> val 区域 = 区域(dbc.getLangaugeByID(SELECTED_LANGUAGE_ID).获取缩写()),我收到了"未解决的引用"错误。

将 Java 类转换为 Kotlin 的最佳方式是什么?

我期望能够使用该方法,或者等价的替代方法。

英文:

I have two classes, one of which extends the other

First class:

open class DataBaseObject @JvmOverloads constructor(
    ctx: Context,
    val iD: Int,
    val name: String,
    displayName: String = ""
) {
    var displayName: String? = null

    init {
        if (displayName === "") {
            val wasteStringKey = ctx.resources.getIdentifier(name, "string", ctx.packageName)
            if (wasteStringKey != 0) this.displayName =
                ctx.getString(wasteStringKey) else this.displayName =
                name.replace("_", " ")
        } else this.displayName = displayName
    }

    open fun getDisplayName(): String? {
        return displayName
    }

    open fun getID(): Int {
        return iD
    }

    open fun getName(): String? {
        return name
    }
}

The second(android version):

public class Language extends DataBaseObject
{
	private String _abbreviation;
	
	public Language(Context ctx, int id, String abbreviation, String name)
	{
		super(ctx, id, name);
		
		_abbreviation = abbreviation;
	}
	
	public String getAbbreviation()
	{
		return _abbreviation;
	}
}

But if I copy paste the code into my class, the IDE converts me to kotlin like this:

(Kotlin version)

open class Language(ctx: Context?, id: Int, val abbreviation: String, name: String?) :
    DataBaseObject(ctx!!, id, name!!)

In another class I use the function
getAbbreviation() -> val locale = Locale(dbc.getLangaugeByID(SELECTED_LANGUAGE_ID).getAbbrevation()) and I get the Unresolved Reference error.
What is the best way to convert java class to kotlin?

I expect to be able to use that method, or an equivalent alternative

答案1

得分: 1

仅需使用

val locale = Locale(dbc.getLangaugeByID(SELECTED_LANGUAGE_ID).abbreviation)

在 Kotlin 中,不习惯使用 getter 方法,通常直接访问属性。实际上,如果你将它保留为 Java 类,你可以通过在 Kotlin 中简单地写入 `abbreviation` 来调用 `getAbbreviation()`,它会自动工作,而且 IDE 可能会建议你使用该方法,而不是使用 getter。这被称为属性访问语法。与当你可以在 `Activity` 中写入 `getApplication()` 时,它会建议你只使用 `application` 一样。参见

[![在这里输入图片描述][1]][1]

从这个角度来看,生成的类与 Java 类相同。请注意,你不需要担心也可以以这种方式设置 `abbreviation`,因为这是不可能的,因为它是一个 `val`。就像只有一个 getter 一样。

  [1]: https://i.stack.imgur.com/MrXOl.png
英文:

simply use

val locale = Locale(dbc.getLangaugeByID(SELECTED_LANGUAGE_ID).abbreviation)

In kotlin it's not idiomatic to use getter methods and you typically just access the properties directly. In fact if you left it as a Java class you could call getAbbreviation() by simply writing abbreviation from Kotlin and it works automatically, and the IDE most likely even suggests you to write that instead of using the getter. This is called property access syntax. The same as when you can write getApplication() inside an Activity it will recommend you to just use application. See

Android 到 Kotlin 的最佳实践转换

In that respect is the generated class identical to the Java class. Note that you don't need to worry that maybe the abbreviation can be set that way also, because that's not possible since it's a val. It's just like having only a getter

huangapple
  • 本文由 发表于 2023年6月29日 16:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定