0

Consider the following code snippet:

void main() {
  Band myBand = new Band();
  myBand.guitar();
  print("Number of players " + myBand.players.toString());
}

class Rock {
  int players=2;
  void guitar() {
    print('Rock guitar');
  }
}

mixin Jazz {
  int players=1;
  void guitar() {
    print('Jazz guitar');
  }
}

class Band extends Rock with Jazz {

}

Here I have the class Band which extends Rock and the mixin Jazz. Both have a property and a method of the same name - players and guitar.

If I now subclass Band and invoke the guitar method or query the player property I get:

Jazz guitar Number of players 1

Obviously for some reason the mixin is of higher importance. What do I have to do to call the properties or the methods of the extending class Rock and not from the mixin, from my subclass myBand?

obscure
  • 11,916
  • 2
  • 17
  • 36
  • Here's a got post abouut mixin and how the order works: https://medium.com/flutter-community/https-medium-com-shubhamhackzz-dart-for-flutter-mixins-in-dart-f8bb10a3d341 – Mariano Zorrilla Mar 04 '20 at 00:36

1 Answers1

0

Mixins in Dart work by creating a new class that layers the implementation of the mixin on top of a superclass to create a new class — it is not “on the side” but “on top” of the superclass, so there is no ambiguity in how to resolve lookups.

Check When to use mixins and when to use interfaces in Dart?

Yauhen Sampir
  • 1,989
  • 15
  • 16