[英]Is it possible to extend a class with a default private constructor in Dart?
Suppose that we have a class a
: 假设我们有a class a
:
class A {
A._();
}
Its default constructor is private : A._()
. 它的默认构造函数是private : A._()
。
Is there any way to extends
that class? 有什么办法可以extends
该类吗?
class B extends A {
}
This results in a compiler error : 这导致编译器错误 :
The superclass 'A' doesn't have a zero argument constructor.
Trying to compose any constructor for B
myself ( B()
) results in another error: 尝试为B
我自己构造任何构造函数( B()
)都会导致另一个错误:
The superclass 'A' doesn't have an unnamed constructor.
No, there is no way. 不,没有办法。 This is an effective way to prevent extending. 这是防止扩展的有效方法。
What you still can do is implementing the class. 您仍然可以执行该类。
class B implements A {}
If the class also has a public non-factory constructor, you can still extend it by forwarding the constructor call to such a named constructor of the super class. 如果该类还具有公共的非工厂构造函数,则仍可以通过将构造函数调用转发到此类超类的命名构造函数来对其进行扩展。
class B extends A {
B() : super.other();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.