1.class.ts
//1.class
console.log('1.class');
class House {
//private door: number;
constructor(public door: number) {
this.door = door;
}
getDoor(width: number, height: number) {
console.log('size: ' + (width * height * this.door));
}
public retriveDoor() {
return this.door;
}
}
let house = new House(5);
house.getDoor(20, 30);
class NewHouse extends House {
name: string;
constructor(door: number, name: string) {
super(door);
this.name = name;
}
getDoor(width: number, height: number) {
console.log('name: ' + this.name);
console.log('door: ' + super.retriveDoor());
super.getDoor(width, height);
}
}
let newHouse = new NewHouse(5, 'Grace');
newHouse.getDoor(25, 30);
//2.class
console.log('2.abstract class');
abstract class Shape {
abstract area(): number;
myname(): void {
console.log('My name is shape');
}
}
class Rect extends Shape {
area(): number {
return 100;
}
}
let shape: Shape;
//error TS2511: Cannot create an instance of the abstract class 'Shape'
//shape = new Shape();
shape = new Rect();
console.log('shape.area(): ' + shape.area());
2.compiler
tsc class.ts
3.產出的 class.js
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
//1.class
console.log('1.class');
var House = /** @class */ (function () {
//private door: number;
function House(door) {
this.door = door;
this.door = door;
}
House.prototype.getDoor = function (width, height) {
console.log('size: ' + (width * height * this.door));
};
House.prototype.retriveDoor = function () {
return this.door;
};
return House;
}());
var house = new House(5);
house.getDoor(20, 30);
var NewHouse = /** @class */ (function (_super) {
__extends(NewHouse, _super);
function NewHouse(door, name) {
var _this = _super.call(this, door) || this;
_this.name = name;
return _this;
}
NewHouse.prototype.getDoor = function (width, height) {
console.log('name: ' + this.name);
console.log('door: ' + _super.prototype.retriveDoor.call(this));
_super.prototype.getDoor.call(this, width, height);
};
return NewHouse;
}(House));
var newHouse = new NewHouse(5, 'Grace');
newHouse.getDoor(25, 30);
//2.class
console.log('2.abstract class');
var Shape = /** @class */ (function () {
function Shape() {
}
Shape.prototype.myname = function () {
console.log('My name is shape');
};
return Shape;
}());
var Rect = /** @class */ (function (_super) {
__extends(Rect, _super);
function Rect() {
return _super !== null && _super.apply(this, arguments) || this;
}
Rect.prototype.area = function () {
return 100;
};
return Rect;
}(Shape));
var shape;
//error TS2511: Cannot create an instance of the abstract class 'Shape'
//shape = new Shape();
shape = new Rect();
console.log('shape.area(): ' + shape.area());
4.執行結果

