Lit*_*pea 7 generics android dart flutter
当我尝试创建一些接口并需要使用泛型来实现它们时,我遇到了问题。 我的问题是如何使用类似的无类型
Kotlin 中没有任何内容
Java 中无效
例子:
我有一个基类
abstract class Base<Out, In> {
Future<Out> perform(In);
}
Run Code Online (Sandbox Code Playgroud)
我用的是正常情况,完美
class Download implements Base<bool, String> {
@override
Future<bool> perform(downloadUrl) async {
// do download and return result here
}
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我不需要包含任何参数。但它需要 2 个泛型类型,所以我必须包含 void 作为输入
class DoSomething implements Base<bool, void> {
@override
Future<bool> perform(_) async {
// do some thing here, no use the input parameter
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当在 DoSomething 类中调用执行函数时,它会将 void 视为与 Java 不同的函数类型,它需要一个输入参数,而不是什么都没有
DoSomething().perform((){});
Run Code Online (Sandbox Code Playgroud)
我不想在执行函数中包含任何参数。请给我任何处理此案的建议和解决方案。
您可以使用
function({dynamic optional_parameter})
Run Code Online (Sandbox Code Playgroud)
它是一个可选参数,如果您不想处理/使用它,您可以简单地忽略它,并且它将为空。
| 归档时间: |
|
| 查看次数: |
1228 次 |
| 最近记录: |