我在java中使用泛型通常帶有介面。我想使用像java這樣的泛型。我該怎么辦。我認為使用動態不能解決我的問題。
uj5u.com熱心網友回復:
你可以這樣做:
/// 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);
}
在 DartPad 上試用完整代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405257.html
標籤:
