简体   繁体   中英

is there a better way to call overridden behavior functions than super.test()?

I have abstracted a bunch of code to behaviors, and now when i do:

class B extends A with behave {
  B():super(){}
}

class A extends PolymerElement{
  A(){}
}

abstract class behave {
  test(){ print("Test"); }
}

So what I have been trying to do is create a workflow without having to append references to this new function test

As of right now, if you implement test in A or B, it will override the behavior I had created. But I was hoping to append more to it, something similar to:

class B extends A with behave {
  B():super(){}

  test():super.test(){}
}

and this would do something like call the parent test. Now when looking this, i would say, this would make sense if the behavior was in the parent. So lets test that out.

abstract class behave { ... }
class A extends behave { ... }
class B extends A {
  test(){
    super.test();
  }
}

This would work and execute what I was wanting to do... Why cant i reference it in the instantiation? test():super.test(){ ... } It seems that doing as just stated will error as a constructor error.

Now what if we put it back to my original design, as behave being with B

abstract class behave { ... }
class A { ... }
class B extends A with behave { 
  test(){
    super.test();
    print("Foo");
  }
}

now here it seems to work as expected, requiring us to create a super reference to this behavior.

Is there an idea of using : for referencing a parent function call, or is this only ever used for constructors? I would say, yes it is only used for constructors for now, but why not append additional functionality. If i wanted to create a series of functions in the behavior which mimic the child implementation, I should either run super.test() either at the top of bottom of the function depending on the order required to function?

Is there something I am missing in dart when reading the docs, or is this how it is suppose to work for the time being?

I doubt that foo() : super.foo() syntax will be added to the language. The : for constructor initializers is useful because the initializers can be analyzed at compile time, which make it simple to verify that final fields are set for instance. The : syntax in a function would just be syntactic sugar for putting that function call at the beginning of the function, which doesn't seem to add much value.

By the way, you can add @mustCallSuper to your the test() function. This will add a check to the linter that all methods that override test() must call super.test() .

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