为什么 Int 不是接口内的 Int?
why is an Int not an Int inside an interface?
我在界面中有这个功能
interface test<Int>{
fun printInt():Int {
return 2}}
错误是:整数文字不符合预期的 Int 类型。
- 如果我将 return 类型更改为
kotlin.Int
,错误就会消失。
interface test<Int>{
fun printInt(): **kotlin.Int** {
return 2}}
- 我不使用 return,它像这样工作正常:
interface test<Int>{
fun printInt() = 2
}
- 如果我从接口中获取 printInt 函数,编译器不会报错:
fun printInt(): **Int** {
return 2}
这些 Int#1 和 kotlin.Int 是什么?
此声明:
interface test<Int>
声明一个 generic interface, with a type parameter called Int
. This is similar to how MutableList
是一个通用接口,带有一个名为 E
:
的类型参数
public interface MutableList<E>
因此在接口内部,非限定名“Int
”指的是类型参数,(类似于MutableList
里面的方式,E
指的是类型参数)不是kotlin.Int
类型。
我在界面中有这个功能
interface test<Int>{
fun printInt():Int {
return 2}}
错误是:整数文字不符合预期的 Int 类型。
- 如果我将 return 类型更改为
kotlin.Int
,错误就会消失。
interface test<Int>{
fun printInt(): **kotlin.Int** {
return 2}}
- 我不使用 return,它像这样工作正常:
interface test<Int>{
fun printInt() = 2
}
- 如果我从接口中获取 printInt 函数,编译器不会报错:
fun printInt(): **Int** {
return 2}
这些 Int#1 和 kotlin.Int 是什么?
此声明:
interface test<Int>
声明一个 generic interface, with a type parameter called Int
. This is similar to how MutableList
是一个通用接口,带有一个名为 E
:
public interface MutableList<E>
因此在接口内部,非限定名“Int
”指的是类型参数,(类似于MutableList
里面的方式,E
指的是类型参数)不是kotlin.Int
类型。