mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Added inheritance and overwrite examples
This commit is contained in:
parent
7f2256b2e6
commit
7d0adf66ea
@ -83,8 +83,10 @@ class Point {
|
|||||||
//Properties
|
//Properties
|
||||||
x: number;
|
x: number;
|
||||||
|
|
||||||
//Constructor - the public keyword is a shortcut to generate the code for a property and it's initialization, equivalent to "x" in this case
|
//Constructor - the public/private keywords are shortcuts to generate the code for a property and its initialization
|
||||||
constructor(x: number, public y: number) {
|
//Equivalent to "x" in this case
|
||||||
|
//Default values are also supported
|
||||||
|
constructor(x: number, public y: number = 0) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +97,23 @@ class Point {
|
|||||||
static origin = new Point(0, 0);
|
static origin = new Point(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = new Point(10 ,20);
|
var p1 = new Point(10 ,20);
|
||||||
|
var p2 = new Point(25); //y will be 0
|
||||||
|
|
||||||
|
//Inheritance
|
||||||
|
class Point3D extends Point {
|
||||||
|
constructor(x: number, y: number, public z: number = 0) {
|
||||||
|
super(x, y); //Explicit call to the super class constructor is mandatory
|
||||||
|
}
|
||||||
|
|
||||||
|
/Overwrite
|
||||||
|
dist() {
|
||||||
|
var d = super.dist();
|
||||||
|
return Math.sqrt(d * d + this.z * this.z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Modules
|
||||||
|
|
||||||
//Generics
|
//Generics
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user