Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Vehicle {
- factory Vehicle(String type, String model) {
- if(type == 'car') return Car(model);
- if(type == 'moto') return Moto(model);
- throw 'can\'t create type $type';
- }
- String get modelName;
- }
- class Car implements Vehicle {
- final String model;
- Car(this.model);
- String get modelName => model;
- }
- class Moto implements Vehicle {
- final String model;
- Moto(this.model);
- String get modelName => model;
- }
- void main() {
- try {
- final v1 = Vehicle('car', 'mercedes');
- final v2 = Vehicle('moto', 'harley');
- print(v1.modelName);
- print(v2.modelName);
- } catch(err) {
- print('i am in the catch close.');
- print(err);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement