class Pointer{ x!:number; // 实例上的属性必须先声明 y!:number; constructor(x:number,y?:number,...args:number[]){ this.x = x; this.y = y as number; } } let p = new Pointer(100,200);
class Animal { public name!: string; // 不写public默认也是公开的 public age!: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } class Cat extends Animal { constructor(name: string, age: number) { super(name, age); console.log(this.name,this.age); // 子类访问 } } let p = new Cat('Tom', 18); console.log(p.name,p.age); // 外层访问 class Animal { constructor(public name: string, public age: number) { this.name = name; this.age = age; } }
我们可以通过参数属性来简化父类中的代码
protected修饰符 (自己和子类可以访问到)
class Animal { constructor(protected name: string, protected age: number) { this.name = name; this.age = age; } } class Cat extends Animal { constructor(name: string, age: number) { super(name, age); console.log(this.name, this.age) } } let p = new Cat('Tom', 18); console.log(p.name,p.age);// 无法访问
class Animal { constructor(private name: string, private age: number) { this.name = name; this.age = age; } } class Cat extends Animal { constructor(name: string, age: number) { super(name, age); console.log(this.name, this.age); // 无法访问 } } let p = new Cat('Tom', 18); console.log(p.name,p.age);// 无法访问
class Animal { constructor(public readonly name: string, public age: number) { this.name = name; this.age = age; } changeName(name:string){ this.name = name; // 仅读属性只能在constructor中被赋值 } } class Cat extends Animal { constructor(name: string, age: number) { super(name, age); } } let p = new Cat('Tom', 18); p.changeName('Jerry');
class Animal { static type = '哺乳动物'; // 静态属性 static getName() { // 静态方法 return '动物类'; } private _name: string = 'Tom'; get name() { // 属性访问器 return this._name; } set name(name: string) { this._name = name; } } let animal = new Animal(); console.log(animal.name);
class Animal { say(message:string){ console.log(message); } static getType(){ return '动物' } } class Cat extends Animal { say(){ // 原型方法中的super指代的是父类的原型 super.say('猫猫叫'); } static getType(){ // 静态方法中的super指代的是父类 return super.getType() } } let cat = new Cat(); console.log(Cat.getType())
function addSay(target:any){ target.prototype.say = function(){console.log('say')} } @addSay class Person { say!:Function } let person = new Person person.say();
// target 是类的原型,,key 是需要修饰的属性 function toUpperCase(target:any,key:string){ let value = target[key]; Object.defineProperty(target,key,{ // 原型定义属性 get(){ return value.toUpperCase(); }, set(newValue){ value = newValue } }) } function double(target: any, key: string) { let value = target[key]; Object.defineProperty(target, key, { get() { return value * 2; }, set(newValue) {value = newValue} }) } class Person { @toUpperCase name: string = 'JiangWen' @double static age: number = 10; getName() { return this.name; } } let person = new Person(); console.log(person.getName(),Person.age)
// 设置某些方法属性是否可用 function noEnum(target:any,key:string,descriptor:PropertyDescriptor){ console.log(descriptor) descriptor.enumerable = false; } class Person { @toUpperCase name: string = 'JiangWen' @double static age: number = 10; @noEnum getName() { return this.name; } } let person = new Person(); console.log(person); // getName 不可枚举
function addPrefix(target:any,key:string,paramIndex:number){ console.log(target,key,paramIndex); // Person.prototype getName 0 } class Person { @toUpperCase name: string = 'JiangWen' @double static age: number = 10; prefix!:string @noEnum getName(@addPrefix prefix:string) { return this.name; } }
abstract class Animal{ name!:string; abstract speak():void } class Cat extends Animal { speak(){ console.log('猫猫叫'); } } class Dog extends Animal{ speak():string{ console.log('汪汪叫'); return 'wangwang' } }