类字段具有不同参数类型(setter 方法重载)

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

Class field with different argument types (setter overloading)

问题

在Kotlin中,你无法像在Java中那样直接将整数分配给Long字段,因为Kotlin会进行更严格的类型检查。你可以通过使用类型转换来实现这一功能。以下是你的代码的翻译部分:

class MyClass {
    var myNumber: Long = 0L // 内部 setMyNumber(value: Long)

    fun setMyNumber(newNumber: Int) {
        myNumber = newNumber.toLong()
    }
}

要将整数分配给myNumber字段,你可以使用以下代码:

val myClass = MyClass()
myClass.myNumber = 4 // 这会调用setMyNumber(newNumber: Int)方法并将整数值分配给myNumber字段

这将调用setMyNumber(newNumber: Int)方法,并将整数值分配给myNumber字段。这样,你可以实现你的需求并保持属性表示法。

英文:

I have a class with a field of type long and I would like to pass either an Int or a Long value.

So I thought I can make a second setter with the same name, but different argument.
Kotlin does not complain and I can even call both setters from Java (same name, one automatically created with long from Kotlin). In Java I just call setMyNumber(long or int) value and the compiler will assign the correct method.

But why can't I do myNumber = 4 in Kotlin, why does it not call the other setter?
Is there a different way I can achieve this functionality, but still keep the property notation (yes I know I can write to setter methods, but then I have to call them with a method call rather just assigning a value)?

class MyClass {

	var myNumber: Long = 0L // internal setMyNumber(value: Long)

	fun setMyNumber(newNumber: Int) {
		myNumber = newNumber.toLong()
	}

}

答案1

得分: 2

截止到目前,你正在尝试的操作是不受支持的。 (参见: 允许属性的setter重载)

一个解决方法是使用超类来表示所有平台类的数值:

class MyClass {
    var myNumber: Number = 0L
        set (value) { field = value.toLong() }
}

val myClass = MyClass()

val anInt: Int = 1
val aLong: Long = 1L

myClass.myNumber = anInt
myClass.myNumber = aLong

[在线尝试!](https://tio.run/##jU69CsIwEN7zFDcmS3FwkGAFcRKqi08QNS3B9CpJWiilz14vabspeMMdfN99P68mWIPT9LDKe7j0p3QHBjSdclD317a@aydhvpDDpkhsHK8D8E7ZVgsYoDTaPukhAVloigYrLmBkI2OsbBFqZZArV3kJR@dUv78FZ7A6iBRIMoqbC@RrFS7YQik8Y5BAi9jtCsYMCXETuivi8@KRrdWJSNovVGZ14FK@qUWwKH6po/0f6nGaPg "Kotlin – 在线尝试")

英文:

As of writing, what you're trying to do is not supported. (See: Allow setters overloading for properties)

A workaround would be using the Superclass for all platform classes representing numeric values:

class MyClass {
    var myNumber: Number = 0L
        set (value) { field = value.toLong() }
}

val myClass = MyClass()

val anInt: Int = 1
val aLong: Long = 1L

myClass.myNumber = anInt
myClass.myNumber = aLong

[Try it online!](https://tio.run/##jU69CsIwEN7zFDcmS3FwkGAFcRKqi08QNS3B9CpJWiilz14vabspeMMdfN99P68mWIPT9LDKe7j0p3QHBjSdclD317a@aydhvpDDpkhsHK8D8E7ZVgsYoDTaPukhAVloigYrLmBkI2OsbBFqZZArV3kJR@dUv78FZ7A6iBRIMoqbC@RrFS7YQik8Y5BAi9jtCsYMCXETuivi8@KRrdWJSNovVGZ14FK@qUWwKH6po/0f6nGaPg "Kotlin – Try It Online")

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

发表评论

匿名网友

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

确定