Last active
February 26, 2020 18:43
-
-
Save GerardKetuma/7d915b3002e309d4ce6b15ec7e995009 to your computer and use it in GitHub Desktop.
Classes in TypeScript
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
// See blog post at https://ketuma.com/lessons/typescript-classes | |
//Classes | |
interface IMatrix { | |
width: number | |
height: number | |
content: string[] | |
dimensions: string | |
getElement(x: number, y: number): string | |
setElement(x: number, y: number, value: string): void | |
printMatrix(): string | |
} | |
interface SetMatrixFunc { | |
(x: number, y: number): string | |
} | |
interface CMatrix { | |
new (width: number, height: number, element: SetMatrixFunc): IMatrix | |
} | |
const Matrix: CMatrix = class Matrix implements IMatrix { | |
width: number | |
height: number | |
content: string[] = [] | |
private setterFunction: (x: number, y: number) => string | |
constructor( | |
w: number, | |
h: number, | |
el: (x: number, y: number) => string, | |
) { | |
this.width = w | |
this.height = h | |
this.setterFunction = el | |
this.initializeMatrix(this.setterFunction) | |
} | |
private initializeMatrix(el: (x: number, y: number) => string): void { | |
for (let y = 0; y < this.height; y++) { | |
for (let x = 0; x < this.width; x++) { | |
this.content[y * this.width + x] = el(x, y) | |
} | |
} | |
} | |
get dimensions(): string { | |
return `${this.width} x ${this.height}` | |
} | |
set dimensions(str: string) { | |
//The string passed will contain the dimension in this format: 'x-y' | |
let [x, y] = str.split('-') | |
this.width = parseInt(x, 10) | |
this.height = parseInt(y, 10) | |
this.initializeMatrix(this.setterFunction) | |
} | |
getElement(x: number, y: number): string { | |
return this.content[y * this.width + x] | |
} | |
setElement(x: number, y: number, value: string): void { | |
this.content[y * this.width + x] = value | |
} | |
printMatrix(): string { | |
let row = '' | |
for (let x = 0; x < this.width; x++) { | |
for (let y = 0; y < this.height; y++) { | |
row += ` ${this.getElement(x, y)}` | |
} | |
row += '\n' | |
} | |
return row | |
} | |
} | |
let matrix = new Matrix(3, 2, (x, y) => `${x}${y}`) | |
console.log(matrix.printMatrix()) // 00 01 10 11 20 21 | |
console.log(matrix.dimensions) // 3 x 2 | |
matrix.dimensions = '3-3' | |
console.log(matrix.width) // 3 | |
console.log(matrix.dimensions) // 3 x 3 | |
console.log(matrix.printMatrix()) // 00 01 02 10 11 12 20 21 22 | |
//--------------------------------------------------------------- | |
interface CSymMatrix { | |
new (size: number, el: SetMatrixFunc): IMatrix | |
} | |
const SymmetricMatrix: CSymMatrix = class SymmetricMatrix extends Matrix | |
implements IMatrix { | |
constructor(size: number, el: (x: number, y: number) => string) { | |
super(size, size, (x, y) => { | |
if (x < y) return el(y, x) | |
else return el(x, y) | |
}) | |
} | |
setElement(x: number, y: number, value: string): void { | |
super.setElement(x, y, value) | |
if (x < y) { | |
super.setElement(y, x, value) | |
} | |
} | |
} | |
let sMatrix = new SymmetricMatrix(3, (x, y) => `${x}${y}`) | |
console.log(sMatrix.printMatrix()) // 00 10 20 10 11 21 20 21 22 | |
//--------------------------------------------------------------- | |
interface CSymMatrix { | |
new (size: number, el: SetMatrixFunc): IMatrix | |
instances: number | |
} | |
const SymmetricMatrix: CSymMatrix = class SymmetricMatrix extends Matrix | |
implements IMatrix { | |
static instances = 0 | |
constructor(size: number, el: (x: number, y: number) => string) { | |
super(size, size, (x, y) => { | |
if (x < y) return el(y, x) | |
else return el(x, y) | |
}) | |
SymmetricMatrix.instances++ | |
} | |
setElement(x: number, y: number, value: string): void { | |
super.setElement(x, y, value) | |
if (x < y) { | |
super.setElement(y, x, value) | |
} | |
} | |
} | |
let sMatrix = new SymmetricMatrix(3, (x, y) => `${x}${y}`) | |
console.log(sMatrix.printMatrix()) // 00 10 20 10 11 21 20 21 22 | |
let bMatrix = new SymmetricMatrix(2, (x, y) => `${x}${y}`) | |
console.log(bMatrix.printMatrix()) // 00 10 10 11 | |
console.log(SymmetricMatrix.instances) // 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment