Created
December 20, 2022 06:39
-
-
Save webmstk/98e958bcbec7f53d2b6fb0372110edce to your computer and use it in GitHub Desktop.
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
// 1. | |
class User { | |
User(this.name, this.surname); | |
String name; | |
String surname; | |
@override | |
String toString() { | |
return 'User(name: $name, surname: $surname)'; | |
} | |
} | |
class Student extends User { | |
Student(String name, String surname, this.yearOfAdmission) | |
: super(name, surname); | |
DateTime yearOfAdmission; | |
int get currentCourse { | |
return DateTime.now().year - yearOfAdmission.year; | |
} | |
@override | |
String toString() { | |
return '${super.toString()} / ' | |
'Student(yearOfAdmission: $yearOfAdmission, currentCourse: $currentCourse)'; | |
} | |
} | |
// 2. | |
abstract class Car with Painter { | |
Car(Colors color) { | |
setColor(color); | |
} | |
double get weight; | |
void run(); | |
@override | |
String toString() { | |
return 'colorName: $colorName'; | |
} | |
} | |
class Truck extends Car with Engine { | |
Truck(Colors color, int engine) : super(color) { | |
enginePower = engine; | |
} | |
@override | |
double get weight => 2; | |
@override | |
void run() { | |
runEngine(); | |
} | |
@override | |
String toString() { | |
return 'Грузовик $colorName цвета'; | |
} | |
} | |
class Sportcar extends Car with Engine { | |
Sportcar(Colors color, int engine) : super(color) { | |
enginePower = engine; | |
} | |
@override | |
double get weight => 2; | |
@override | |
void run() { | |
runEngine(); | |
} | |
@override | |
String toString() { | |
return 'Гоночная машина $colorName цвета'; | |
} | |
} | |
class Bike with Painter {} | |
enum Colors { red, green, blue } | |
mixin Painter { | |
late String colorName; | |
void setColor(Colors color) => colorName = color.name; | |
} | |
mixin Engine on Car { | |
int enginePower = 200; | |
void runEngine() { | |
print( | |
'Двигатель работает. Максимальная скорость = ${enginePower ~/ weight}'); | |
} | |
} | |
// 3. | |
class Stringer<T> { | |
String toStr(T value) { | |
return value.toString(); | |
} | |
} | |
void main() { | |
// 1. | |
DateTime yearOfAdmission = DateTime(2019); | |
final student = Student('Pavel', 'Bakun', yearOfAdmission); | |
print(student); | |
// 2. | |
final truck = Truck(Colors.green, 170); | |
print(truck); | |
truck.run(); | |
final sportCar = Sportcar(Colors.red, 400); | |
print(sportCar); | |
sportCar.run(); | |
// 3. | |
final stringer = Stringer(); | |
print(stringer.toStr(123)); | |
print(stringer.toStr(123.0)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment