Added inheritance and overwrite examples

This commit is contained in:
Philippe 2014-08-14 15:56:30 +02:00
parent 7f2256b2e6
commit 7d0adf66ea

View File

@ -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