mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 15:24:09 +03:00
Added inheritance and overwrite examples
This commit is contained in:
parent
7f2256b2e6
commit
7d0adf66ea
@ -83,8 +83,10 @@ class Point {
|
||||
//Properties
|
||||
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(x: number, public y: number) {
|
||||
//Constructor - the public/private keywords are shortcuts to generate the code for a property and its initialization
|
||||
//Equivalent to "x" in this case
|
||||
//Default values are also supported
|
||||
constructor(x: number, public y: number = 0) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
@ -95,7 +97,23 @@ class Point {
|
||||
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user