[英]How to use generics in dart。
I use generics in java usually with interface.我在 java 中使用 generics 通常带有接口。 I want to use generics like java.
我想像 java 一样使用 generics。 what should I do.
我应该怎么办。 I think use dynamic can not solve my issues.
我认为使用动态不能解决我的问题。
You could do something like this:你可以这样做:
/// To make an interface in Dart you can create an abstract class.
///
/// Then you can define a generic type with <T>.
///
/// In this sample I'm using <T extends Object> to ensure
/// that I'm not passing a nullable type (such as String?).
abstract class MyInterface<T extends Object> {
T get myValue;
}
/// Then you can implement your interface like this.
class MyInterfaceImplementation implements MyInterface<String> {
@override
final String myValue;
MyInterfaceImplementation(this.myValue);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.