You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
2.9 KiB
Dart
145 lines
2.9 KiB
Dart
import 'dart:math';
|
|
|
|
void basic_operators() {
|
|
print("\nDart Operators:");
|
|
// Type test operators
|
|
double n = 1.1;
|
|
print((n as num).toString());
|
|
print(n is! int);
|
|
print(n is double);
|
|
|
|
var a = Account(username: 'Bobson');
|
|
print(a);
|
|
|
|
var b = Account();
|
|
b?.username = "I'm Groot";
|
|
print(b);
|
|
|
|
var list = List()
|
|
..insert(0, 123)
|
|
..add(456)
|
|
..addAll([789, 111]);
|
|
print(list);
|
|
}
|
|
|
|
class Account {
|
|
String username;
|
|
String password;
|
|
|
|
Account({username, password})
|
|
: this.username = username,
|
|
// this.password = (password != null) ? password : 'AutomaticallyGenerateSecretPassword';
|
|
this.password = password ?? 'AutomaticallyGenerateSecretPassword';
|
|
|
|
@override
|
|
String toString() {
|
|
return "Account($username, $password)";
|
|
}
|
|
}
|
|
|
|
void basic_classes() {
|
|
print("\nDart Classes:");
|
|
// Point Class from dart:math
|
|
var p1 = Point(1, 2);
|
|
var p2 = Point(3, 4);
|
|
print("p1: $p1, p2: $p2");
|
|
print(p1.distanceTo(p2));
|
|
print(p2.magnitude);
|
|
|
|
// Complex Number (複數) - x + yi (i = √-1)
|
|
|
|
// 0 + 0i
|
|
var c1 = BaseComplex();
|
|
c1.real = 5;
|
|
c1.imaginary = 2;
|
|
print(c1.runtimeType);
|
|
|
|
var c2 = BaseComplex()
|
|
..real = 5
|
|
..imaginary = 2;
|
|
print("c1: $c1, c2: $c2");
|
|
|
|
print(c1 == c2);
|
|
|
|
// With Constructor
|
|
var c = Complex(5, 2);
|
|
var r = Complex.real(10);
|
|
var i = Complex.imaginary(-5);
|
|
print("c: $c, r: $r, i: $i");
|
|
}
|
|
|
|
class BaseComplex {
|
|
num real;
|
|
num imaginary;
|
|
|
|
@override
|
|
bool operator ==(dynamic other) {
|
|
if (other is! BaseComplex) {
|
|
return false;
|
|
} else {
|
|
return real == other.real && imaginary == other.imaginary;
|
|
}
|
|
}
|
|
|
|
// @override
|
|
// String toString() {
|
|
// if (imaginary >= 0) {
|
|
// return "$real + ${imaginary}i";
|
|
// } else {
|
|
// return "$real - ${imaginary.abs()}i";
|
|
// }
|
|
// }
|
|
|
|
// @override
|
|
// String toString() => "${this.runtimeType}($real, $imaginary)";
|
|
}
|
|
|
|
class Complex {
|
|
// Private property(member)
|
|
num _real;
|
|
num _imaginary;
|
|
|
|
// Getters & Setters
|
|
get real => _real;
|
|
set real(num newReal) => _real = newReal;
|
|
|
|
get imaginary => _imaginary;
|
|
set imaginary(num newImaginary) => _imaginary = newImaginary;
|
|
|
|
// Traditional way
|
|
// Complex(num real, num imaginary) {
|
|
// this.real = real;
|
|
// this.imaginary = imaginary;
|
|
// }
|
|
|
|
// Syntactic sugar
|
|
Complex(this._real, this._imaginary);
|
|
|
|
// Named Constructor
|
|
// Complex.real(num real) {
|
|
// this.real = real;
|
|
// this.imaginary = 0;
|
|
// }
|
|
Complex.real(num real) : this(real, 0);
|
|
|
|
Complex.imaginary(num imaginary) : this(0, imaginary);
|
|
|
|
@override
|
|
bool operator ==(dynamic other) {
|
|
if (other is! Complex) {
|
|
return false;
|
|
} else {
|
|
return real == other.real && imaginary == other.imaginary;
|
|
}
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
if (imaginary >= 0) {
|
|
return "$real + ${imaginary}i";
|
|
} else {
|
|
return "$real - ${imaginary.abs()}i";
|
|
}
|
|
}
|
|
}
|