Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.69 KB | None | 0 0
  1. abstract class Vehicle {
  2.   factory Vehicle(String type, String model) {
  3.     if(type == 'car') return  Car(model);
  4.     if(type == 'moto') return  Moto(model);
  5.     throw 'can\'t create type $type';
  6.   }
  7.   String get modelName;
  8. }
  9.  
  10. class Car implements Vehicle {
  11.   final String model;
  12.   Car(this.model);
  13.   String get modelName => model;
  14. }
  15.  
  16. class Moto implements Vehicle {
  17.   final String model;
  18.   Moto(this.model);
  19.   String get modelName => model;
  20. }
  21.  
  22. void main() {
  23.   try {
  24.     final v1 = Vehicle('car', 'mercedes');
  25.     final v2 = Vehicle('moto', 'harley');
  26.     print(v1.modelName);
  27.     print(v2.modelName);
  28.   } catch(err) {
  29.     print('i am in the catch close.');
  30.     print(err);
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement