简体   繁体   中英

Is it possible to extend a class with a default private constructor in Dart?

Suppose that we have a class a :

class A {
  A._();
}

Its default constructor is private : A._() .

Is there any way to extends that class?

Problem

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:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM