- public/protected/private 关键字与继承
export {}
class Father {
static fatherMoney: number = 100;
public name: string;
protected age: number;
private book: string;
constructor(name: string, age: number, book: string) {
this.name = name;
this.age = age;
this.book = book;
}
}
class Son extends Father {
isSon: boolean;
constructor(name: string, age: number, isSon: boolean, book: string) {
super(name, age, book);
this.isSon = isSon;
}
sendBook() {
}
}
const fa = new Father('zs', 30, '水浒转');
const son = new Son('ls', 18, true, '三国演义');
- 装饰器模式
export {}
namespace a {
function addNameEat(c: Function) {
c.prototype.name = 'zs';
c.prototype.eat = function () {
console.log('eat');
}
}
@addNameEat
class Person {
name: string;
eat: Function;
constructor(){}
}
const p: Person = new Person();
console.log(p.name);
p.eat();
}
namespace b {
function addNameEatFactory(name: string, eat: Function) {
return function addNameEat(c: Function) {
c.prototype.name = name;
c.prototype.eat = eat;
}
}
@addNameEatFactory('ls', function(){console.log('eat more');})
class Person {
name: string;
eat: Function;
constructor(){}
}
const p: Person = new Person();
console.log(p.name);
p.eat();
}