Created
February 18, 2020 14:26
-
-
Save narenarjun/9e17578f78ec4567b2a4dd322815957f to your computer and use it in GitHub Desktop.
explains about dart classes - methods, final,static and basic inheritance
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
void main() { | |
var n1 = Complex(3, -2); | |
var n2 = Complex(1, 4); | |
print(n1 + n2); | |
print(n1.multiply(n2)); | |
print(Complex.substract(n1, n2)); | |
print(Complex.counter); | |
print(Quaternion(1,-2,-3)); | |
} | |
// var c = Complex(3, 8); | |
// var c2 = Complex(9, 2); | |
// var r = Complex.real(10); | |
// var i = Complex.imaginary(3); | |
// i.imaginary = 20; | |
// i.real = 334; | |
// i.real; | |
// i.imaginary; | |
// print(c == c2); | |
// print(c); | |
// print(c2); | |
// print(r); | |
// print(i); | |
class Complex { | |
num _real; | |
num _imaginary; | |
static num counter = 0; | |
get real => _real; | |
set real(num value) => _real = value; | |
get imaginary => _imaginary; | |
set imaginary(num value) => _imaginary = value; | |
// num getReal(){ | |
// return _real; | |
// } | |
// void setReal(num real){ | |
// this._real = real; | |
// } | |
// num getImaginary(){ | |
// return _imaginary; | |
// } | |
// void setImaginary(num imaginary){ | |
// this._imaginary = imaginary; | |
// } | |
Complex(this._real, this._imaginary) { | |
counter = counter + 1; | |
} | |
Complex.real(num real) : this(real, 0); | |
Complex.imaginary(num imaginary) : this(0, imaginary); | |
Complex multiply(Complex c) { | |
var first = this.real * c.real; | |
var inner = this.imaginary * c.real; | |
var outer = this.real * c.imaginary; | |
var last = -(this.imaginary * c.imaginary); | |
return Complex(first + last, inner + outer); | |
} | |
Complex operator +(Complex c) { | |
return Complex( | |
this.real + c.real, | |
this.imaginary + c.imaginary, | |
); | |
} | |
static Complex substract(Complex c1, Complex c2) { | |
return Complex(c1.real - c2.real, c1.imaginary - c2.imaginary); | |
} | |
@override | |
bool operator ==(other) { | |
if (!(other is Complex)) { | |
return false; | |
} | |
return this._real == other.real && this._imaginary == other.imaginary; | |
} | |
@override | |
String toString() { | |
if (_imaginary >= 0) { | |
return "$_real + ${_imaginary}i"; | |
} | |
return "$_real - ${_imaginary.abs()}i"; | |
} | |
} | |
class Quaternion extends Complex { | |
num jImage; | |
Quaternion( | |
num real, | |
num imaginary, | |
this.jImage, | |
) : super( | |
real, | |
imaginary, | |
); | |
@override | |
String toString() { | |
if (this.jImage >= 0 && this._imaginary >= 0) { | |
return '${this._real} + ${this._imaginary}i + ${this.jImage}j '; | |
} | |
if (this.jImage >= 0 && this._imaginary < 0) { | |
return '${this._real} - ${this._imaginary.abs()}i + ${this.jImage}j '; | |
} | |
return '${this._real} - ${this._imaginary.abs()}i - ${this.jImage.abs()}j '; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment