Browse Source

feat: Finish part - 5

master
Bobson Lin 2 years ago
parent
commit
6aed2c9014
2 changed files with 141 additions and 0 deletions
  1. +5
    -0
      bin/main.dart
  2. +136
    -0
      lib/basic_part5.dart

+ 5
- 0
bin/main.dart View File

@ -2,6 +2,7 @@ import 'package:dart_basics/basic_part1.dart' as part1;
import 'package:dart_basics/basic_part2.dart' as part2;
import 'package:dart_basics/basic_part3.dart' as part3;
import 'package:dart_basics/basic_part4.dart' as part4;
import 'package:dart_basics/basic_part5.dart' as part5;
main(List<String> arguments) {
// Part 1
@ -18,4 +19,8 @@ main(List<String> arguments) {
// Part 4
part4.basic_classes();
// Part 5
part5.basic_classes();
part5.basic_interfaces_and_mixins();
}

+ 136
- 0
lib/basic_part5.dart View File

@ -0,0 +1,136 @@
import 'dart:math';
void basic_classes() {
Square square = Square(10.0);
Rectangle rectangle = Rectangle(20.0, 15.0);
Circle circle = Circle(2.0);
print(square.name);
print(rectangle.name);
print(circle.name);
}
abstract class Shape {
double get perimeter;
double get area;
String get name;
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double get area => pi * (radius * radius); // πr²
@override
String get name => "I'm a circle with radius: $radius";
@override
double get perimeter => 2 * pi; // 2πr
@override
String toString() => "${this.runtimeType}($radius)";
}
class Rectangle extends Shape {
double length;
double width;
Rectangle(this.length, this.width);
@override
double get area => length * width;
@override
String get name => "I'm a rectangle with length: $length, width: $width";
@override
double get perimeter => 2 * (length + width);
@override
String toString() => "${this.runtimeType}($length, $width)";
}
class Square extends Rectangle {
Square(double side) : super(side, side);
@override
String get name => "I'm a square with side: $length";
}
void basic_interfaces_and_mixins() {
SmartPhone myPhone = SmartPhone("123-456-789");
SmartTablet myPad = SmartTablet("111-222-333");
try {
myPhone.pickUp();
myPhone.dial("789-456-123");
myPad.pickUp();
myPad.dial("789-456-123");
} catch (e) {
print(e);
}
print(myPhone.nowTime);
myPhone.setAlarm(myPhone.nowTime.add(Duration(minutes: 20)));
}
mixin ClockMixin {
DateTime nowTime = DateTime.now();
void setAlarm(DateTime alarmTime) {
print("Set an alarm at $alarmTime");
}
}
/// Interface Phone
abstract class Phone {
String get phoneNumber;
void dial(String number) {}
void pickUp() {}
}
class SmartPhone with ClockMixin implements Phone {
String _phoneNumber;
SmartPhone(String phoneNumber) : this._phoneNumber = phoneNumber;
@override
void dial(String number) {
print("SmartPhone($phoneNumber) dials $number");
}
@override
String get phoneNumber => _phoneNumber;
@override
void pickUp() {
print("Pick up SmartPhone($phoneNumber)");
}
}
class SmartTablet with ClockMixin implements Phone {
String _phoneNumber;
SmartTablet(String phoneNumber) : this._phoneNumber = phoneNumber;
@override
void dial(String number) {
throw UnimplementedError("SmartTablet can't dial");
}
@override
String get phoneNumber => _phoneNumber;
@override
void pickUp() {
print("Pick up SmartTablet($phoneNumber)");
}
}

Loading…
Cancel
Save