Dart / Flutter - class 不必实现抽象 class 的抽象方法?
Dart / Flutter - class doesn't have to implement abstract class's abstract methods?
在 Flutter 中我们有 class
abstract class PreferredSizeWidget implements Widget {
Size get preferredSize;
}
和
@immutable
abstract class Widget extends DiagnosticableTree {
const Widget({ this.key });
final Key? key;
@protected
@factory
Element createElement();
@override
String toStringShort() {
final String type = objectRuntimeType(this, 'Widget');
return key == null ? type : '$type-$key';
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.defaultDiagnosticsTreeStyle = DiagnosticsTreeStyle.dense;
}
@override
@nonVirtual
bool operator ==(Object other) => super == other;
@override
@nonVirtual
int get hashCode => super.hashCode;
static bool canUpdate(Widget oldWidget, Widget newWidget) {
return oldWidget.runtimeType == newWidget.runtimeType
&& oldWidget.key == newWidget.key;
}
static int _debugConcreteSubtype(Widget widget) {
return widget is StatefulWidget ? 1 :
widget is StatelessWidget ? 2 :
0;
}
}
我的问题是:为什么如果我们混合使用 PreferredSizeWidget class 我们不必实现所有抽象 class 小部件方法和抽象方法?
我试着模拟了一些代码
class A implements B {}
abstract class B extends C {}
abstract class C {
void test() {
print("test");
}
}
出现错误
Error: The non-abstract class 'A' is missing implementations for these members:
- C.test
class A implements B {
^
lib/main.dart:16:8:
Haven’t run it myself, but in your case A isn’t abstract anymore so it
has to implement everything, in the case of PreferredSizeWidget - you
usually extend/implement/ mixin it on a widget (which already
implements everything from widget) thus leaving you with only
preferredSize
@Norbert515的回答。
要点是,即使我们 mixin/extend PreferredSizeWidget 我们实际上实现了所有的 Widget 方法,但是因为我们通常将它混合在 Widget 上,所以我们不必隐式覆盖这些方法.
在 Flutter 中我们有 class
abstract class PreferredSizeWidget implements Widget {
Size get preferredSize;
}
和
@immutable
abstract class Widget extends DiagnosticableTree {
const Widget({ this.key });
final Key? key;
@protected
@factory
Element createElement();
@override
String toStringShort() {
final String type = objectRuntimeType(this, 'Widget');
return key == null ? type : '$type-$key';
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.defaultDiagnosticsTreeStyle = DiagnosticsTreeStyle.dense;
}
@override
@nonVirtual
bool operator ==(Object other) => super == other;
@override
@nonVirtual
int get hashCode => super.hashCode;
static bool canUpdate(Widget oldWidget, Widget newWidget) {
return oldWidget.runtimeType == newWidget.runtimeType
&& oldWidget.key == newWidget.key;
}
static int _debugConcreteSubtype(Widget widget) {
return widget is StatefulWidget ? 1 :
widget is StatelessWidget ? 2 :
0;
}
}
我的问题是:为什么如果我们混合使用 PreferredSizeWidget class 我们不必实现所有抽象 class 小部件方法和抽象方法?
我试着模拟了一些代码
class A implements B {}
abstract class B extends C {}
abstract class C {
void test() {
print("test");
}
}
出现错误
Error: The non-abstract class 'A' is missing implementations for these members:
- C.test
class A implements B {
^
lib/main.dart:16:8:
Haven’t run it myself, but in your case A isn’t abstract anymore so it has to implement everything, in the case of PreferredSizeWidget - you usually extend/implement/ mixin it on a widget (which already implements everything from widget) thus leaving you with only preferredSize
@Norbert515的回答。
要点是,即使我们 mixin/extend PreferredSizeWidget 我们实际上实现了所有的 Widget 方法,但是因为我们通常将它混合在 Widget 上,所以我们不必隐式覆盖这些方法.