Created
January 24, 2020 09:05
-
-
Save AlexVegner/7ae4d9e2b4921a97bdf52696353d528b to your computer and use it in GitHub Desktop.
dart_mixin.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:math' as math; | |
| class Point { | |
| final int x; | |
| final int y; | |
| const Point(this.x, this.y); | |
| } | |
| // class / abstract class can be used as mixin | |
| class LastPoint { | |
| bool last = false; | |
| } | |
| // Mixin depends on class | |
| mixin Distance on Point { | |
| double distance(Point p) { | |
| final a = (x - p.x).abs(); | |
| final b = (y - p.y).abs(); | |
| return math.sqrt(a*a + b*b); | |
| } | |
| } | |
| class PointWithDistance extends Point with Distance, LastPoint { | |
| PointWithDistance(int x, int y) : super(x, y); | |
| } | |
| void main() { | |
| final p1 = PointWithDistance(0, 3); | |
| final p2 = PointWithDistance(4, 0) | |
| ..last = true; | |
| final distance = p1.distance(p2); | |
| print(distance); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment