I am getting an error asking for member declaration and I have no idea why. I have been trying to look things up but can't seem to find an answer to why this is invalid. I can do val listOfArr = ArrayList<Array<String>>()
just fine. Why does this not work?
我收到错误要求成员声明,我不明白为什么。我一直在努力寻找目标,但似乎无法找到为什么这是无效的答案。我可以做val listOfArr = ArrayList
class Implements: IPatternCheck {
override fun <Array<String>> check(classData: ClassData, value: Array<String>): Boolean {
return true
}
}
This is my interface
这是我的界面
interface IPatternCheck {
fun<T> check(classData: ClassData, value: T): Boolean
}
1 个解决方案
#1
3
If your interface declares a function with a type parameter, you have to keep that type parameter intact when you override it - so you'd have to create this override for it in your class:
如果你的接口声明了一个带有type参数的函数,你必须在覆盖它时保持该类型参数不变 - 所以你必须在你的类中为它创建这个覆盖:
override fun <T> check(classData: ClassData, value: T): Boolean {
// TODO
}
To make it take a specific type, you should make your interface generic instead of just the function inside it, and implement the interface with the specific type passed as the type parameter:
要使它采用特定类型,您应该使您的接口通用而不仅仅是其中的函数,并实现具有作为类型参数传递的特定类型的接口:
interface IPatternCheck<T> {
fun check(classData: ClassData, value: T): Boolean
}
class Implements: IPatternCheck<Array<String>> {
override fun check(classData: ClassData, value: Array<String>): Boolean {
// TODO
}
}
Edit, answering the question in the comment below. If you do this:
编辑,回答下面评论中的问题。如果你这样做:
override fun <String> check(classData: ClassData, value: String): Boolean {
// TODO
}
... all you're doing is just renaming the T
type parameter to String
(rather confusingly). You're not actually using the kotlin.String
class that stores a sequence of characters.
...你所做的只是将T类型参数重命名为String(相当令人困惑)。你实际上并没有使用存储一系列字符的kotlin.String类。
For example, you can still call an instance of Implements
with any second parameter, it won't be restricted to a kotlin.String
. It's still a generic function.
例如,您仍然可以使用任何第二个参数调用Implements的实例,它不会被限制为kotlin.String。它仍然是一个通用功能。
val implements = Implements()
implements.check(ClassData(), "foo")
implements.check(ClassData(), 25)
Also, you can't access any kotlin.String
functions on the parameter:
此外,您无法访问参数上的任何kotlin.String函数:
override fun <String> check(classData: ClassData, value: String): Boolean {
value.length // Unresolved reference: length
}
#1
3
If your interface declares a function with a type parameter, you have to keep that type parameter intact when you override it - so you'd have to create this override for it in your class:
如果你的接口声明了一个带有type参数的函数,你必须在覆盖它时保持该类型参数不变 - 所以你必须在你的类中为它创建这个覆盖:
override fun <T> check(classData: ClassData, value: T): Boolean {
// TODO
}
To make it take a specific type, you should make your interface generic instead of just the function inside it, and implement the interface with the specific type passed as the type parameter:
要使它采用特定类型,您应该使您的接口通用而不仅仅是其中的函数,并实现具有作为类型参数传递的特定类型的接口:
interface IPatternCheck<T> {
fun check(classData: ClassData, value: T): Boolean
}
class Implements: IPatternCheck<Array<String>> {
override fun check(classData: ClassData, value: Array<String>): Boolean {
// TODO
}
}
Edit, answering the question in the comment below. If you do this:
编辑,回答下面评论中的问题。如果你这样做:
override fun <String> check(classData: ClassData, value: String): Boolean {
// TODO
}
... all you're doing is just renaming the T
type parameter to String
(rather confusingly). You're not actually using the kotlin.String
class that stores a sequence of characters.
...你所做的只是将T类型参数重命名为String(相当令人困惑)。你实际上并没有使用存储一系列字符的kotlin.String类。
For example, you can still call an instance of Implements
with any second parameter, it won't be restricted to a kotlin.String
. It's still a generic function.
例如,您仍然可以使用任何第二个参数调用Implements的实例,它不会被限制为kotlin.String。它仍然是一个通用功能。
val implements = Implements()
implements.check(ClassData(), "foo")
implements.check(ClassData(), 25)
Also, you can't access any kotlin.String
functions on the parameter:
此外,您无法访问参数上的任何kotlin.String函数:
override fun <String> check(classData: ClassData, value: String): Boolean {
value.length // Unresolved reference: length
}