Last active
May 3, 2018 23:33
-
-
Save Sfshaza/5229b9f21fcfe336a2de51e69d15540f to your computer and use it in GitHub Desktop.
Java-to-Dart codelab: CircleMock example
This file contains 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'; | |
abstract class Shape { | |
factory Shape(String type) { | |
if (type == "circle") return new Circle(2); | |
if (type == "square") return new Square(2); | |
throw new ArgumentError("Can't create $type."); | |
} | |
num get area; | |
} | |
class Circle implements Shape { | |
final num radius; | |
Circle(this.radius); | |
num get area => PI * pow(radius, 2); | |
} | |
class Square implements Shape { | |
final num side; | |
Square(this.side); | |
num get area => pow(side, 2); | |
} | |
class CircleMock implements Circle { | |
num area; | |
num radius; | |
} | |
main() { | |
var circle = new Circle(2); | |
var square = new Square(2); | |
print(circle.area); | |
print(square.area); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will be replaced by https://gist.github.com/kwalrath/410028aef33e1157f7c7d78a8b67b419.