Pergunta

Is there way to overriding method in Dart like JAVA, for example:

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

And when overriding:

A a = new A() {
    @Override
    public void handleLoad() {
        // do some code
    }
};
Foi útil?

Solução

No, Dart does not have anonymous classes. You have to create a class that extends A and instantiate it.

Outras dicas

No but it much less useful in Dart because you can just reassign 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 some extra boilerplate to access instance.

Use type Function:

    class A {
        final Function h
        A(this.h);
        void handleLoad(String loadResult) { h(loadResult); }
    }

Or

    class A {
        final Function handleLoad;
        A(this.handleLoad);
    }

    A a = new A((String loadResult){
        //do smth.
    });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top