Last active
November 29, 2020 04:46
-
-
Save karabanovbs/95436b53cac537b211bb4bbe482bc53f to your computer and use it in GitHub Desktop.
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
// 2.7 Миксины | |
// Есть базовый класс Car, с базовыми свойствами для всех машин | |
// abstract class Car { | |
// double weight; | |
// void run(); | |
// } | |
// Есть два наследника Грузовик и Спорткар | |
// class Truck extends Car { | |
// @override | |
// double get weight = 10; | |
// @override | |
// void run() { } | |
// } | |
// class Sportcar extends Car { | |
// @override | |
// double get weight = 2; | |
// @override | |
// void run() { } | |
// } | |
// И есть велосипед | |
// class Bike{} | |
// Велосипеды и автомобили являются разными видами транспорта, с различными свойствами. Но, и велосипед и автомобили можно покрасить краской! Необходимо написать миксин, устанавливающий на выбор один из трех цветов для транспортного средства | |
// Создайте enum Colors с цветами red, green, blue | |
// Создайти миксин Painter с свойством colorName:String и методом setColor(Colors color). В зависимости от значения enum, метод устанавливает название цвета | |
// 'red', 'green', 'blue' | |
// Примените миксин к транспортным средствам и инициализируйте покраску в конструкторе класса. | |
// Переопределите метод toString, чтобы он выводил "Грузовик color_name цвета" | |
abstract class Car with Painter { | |
double weight; | |
void run(); | |
} | |
class Truck extends Car { | |
Truck() { | |
setColor(Colors.blue); | |
} | |
@override | |
double get weight => 10; | |
@override | |
void run() {} | |
@override | |
String toString() => "Грузовик $colorName"; | |
} | |
class Sportcar extends Car { | |
@override | |
double get weight => 2; | |
@override | |
void run() {} | |
Sportcar() { | |
setColor(Colors.red); | |
} | |
String toString() => "Спорткар $colorName"; | |
} | |
enum Colors { red, green, blue } | |
mixin Painter { | |
String colorName; | |
setColor(Colors color) { | |
switch (color) { | |
case Colors.red: | |
colorName = 'red'; | |
break; | |
case Colors.green: | |
colorName = 'green'; | |
break; | |
case Colors.blue: | |
colorName = 'blue'; | |
break; | |
} | |
} | |
} | |
class Bike with Painter { | |
Bike() { | |
setColor(Colors.green); | |
} | |
String toString() => "Велосипед $colorName"; | |
} | |
void main() { | |
print(Truck()); | |
print(Sportcar()); | |
print(Bike()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment