为什么Kotlin不能覆盖List <*>运算符方法?

jun*_*unk 1 kotlin

这是我的功能:

operator  infix fun  List<Teacher>.get(int: Int): Teacher {
    var t = Teacher()
    t.name = "asd"
    return t ;
}
Run Code Online (Sandbox Code Playgroud)

和我的用法:

b[0].teachers[1].name
Run Code Online (Sandbox Code Playgroud)

提示:b是具有List <Teacher>属性的对象

和错误Empty list doesn't contain element at index 1.

为什么这个覆盖操作员功能不起作用?

hot*_*key 8

在Kotlin中,您不能使用扩展名遮蔽成员函数.一个成员总是赢在电话解决.因此,您基本上不能调用具有与成员函数相同的签名的扩展,该扩展存在于为表达式声明或推断的类型中.

class C {
    fun foo() { println("member") }
}

fun C.foo() { println("extension") }

C().foo() // prints "member"
Run Code Online (Sandbox Code Playgroud)

在您的情况下,成员函数abstract operator fun get(index: Int): E定义在kotlin.collections.List.

请参阅语言参考:扩展已静态解析