Extending a class with just one factory constructor

I was wondering what is the best way to extend the CustomEvent class, a class that has only one factory constructor. I tried to do the following and ran into a problem with the super constructor:

 class MyExtendedEvent extends CustomEvent { int count; factory MyExtendedEvent(num count) { return new MyExtendedEvent._internal(1); } MyExtendedEvent._internal(num count) { this.count = count; } } 

but I can't get it to work. I always come across:

unresolved implicit call to the 'CustomEvent ()' super constructor

If I try to make an internal constructor for:

 MyExtendedEvent._internal(num count) : super('MyCustomEvent') { this.count = count; } 

In the end, I:

'allowed to implicitly call the superstructor' CustomEvent () ''.

I'm not sure what I'm doing wrong - but I think the problem is that CustomEvent has only one constructor, which is the factory constructor (as doc says - http://api.dartlang.org/docs/releases/latest/dart_html /CustomEvent.html )

What is the best way to extend CustomEvent or any class of this form?

+6
source share
2 answers

You cannot directly extend a class using the factory constructor. However, you can implement the class and use delegation to simulate the extension.

For instance:

 class MyExtendedEvent implements CustomEvent { int count; final CustomEvent _delegate; MyExtendedEvent(this.count, String type) : _delegate = new CustomEvent(type); noSuchMethod(Invocation invocation) => reflect(_delegate).delegate(invocation); } 

NB: I used reflection here to simplify a piece of code. A better implementation (in terms of characteristics) would be to define all methods, such as method1 => _delegate.method1()

+10
source

Unfortunately, you cannot extend a class, if it has only factory constructors, you can implement it. This will not work well with CustomEvent, although since it is a DOM type, therefore it only has factory constructors: the browser must create these instances, the Dart object is just a wrapper. If you try to implement CustomElement and run it, you will probably get an error message.

+2
source

Source: https://habr.com/ru/post/952950/


All Articles