Dart override method on the fly (e.g. JAVA)

Is there a way to override a method in Dart as JAVA, for example:

public class A {
    public void handleLoad() {
    }
}

And when overriding:

A a = new A() {
    @Override
    public void handleLoad() {
        // do some code
    }
};
+4
source share
2 answers

No, Dart has no anonymous classes. You must create a class that extends Aand creates it.

+7
source

No, but this is much less useful in Dart, because you can simply reassign the function:

typedef void PrintMsg(msg);

class Printer {
  PrintMsg foo = (m) => print(m);
}

main() {
  Printer p = new Printer()
  ..foo('Hello') // Hello
  ..foo = ((String msg) => print(msg.toUpperCase()))
  ..foo('Hello'); //HELLO
}

However, you will need an additional template to access the instance.

+2
source

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


All Articles