Created
August 1, 2019 15:33
-
-
Save eltray/d6c025896270568719147a738f32b128 to your computer and use it in GitHub Desktop.
Learning Dart - 5 Interfaces
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; | |
abstract class Shape { | |
num get area; | |
factory Shape(String type) { | |
if (type == 'circle') return Circle(2); | |
if (type == 'square') return Square(2); | |
throw 'Can\'t create $type'; | |
} | |
} | |
class Circle implements Shape { | |
final num radius; | |
num get area => math.pi * math.pow(radius, 2); | |
Circle(this.radius); | |
} | |
class CircleMock implements Circle{ | |
num area; | |
num radius; | |
} | |
class Square implements Shape { | |
final num side; | |
num get area => math.pow(side, 2); | |
Square(this.side); | |
} | |
main() { | |
final circle = Shape('circle'); | |
final square = Shape('square'); | |
print(circle.area); | |
print(square.area); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment