微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

初识(typescript)--ts //模块,命名空间,装饰器,例子类,接口

模块

export(类,接口,变量,函数) :导出声明 default(认)

import:导入

命名空间

namespace : 定义一块私有的空间,恰好与export相反,外部看不到内部细节

装饰器

在类的上面@装饰器名称 : 不改变原有的类,动态扩展类的属性方法

function logclass (info : any){
    console.log(info)
    info.prototype.apiurl = 'localhost:8080'
}


@logclass
class use{
    name:string | undefined;
    password : string | undefined;
}

let A = new use();

console.log(A.apiurl);//编译会报错,但是打印结果会出来

例子

// 定义一个泛型接口

interface DB<T>{
    get(id:number): any;
    updata(type:T , id : number):boolean;
    delete(id:number):boolean;
    add(info : T) : T
}

// 实现泛型接口必须是泛型类
class MysqLDB<T> implements DB<T>{
    add(info: T): T {
        throw new Error("Method not implemented.");
    }
    get(id: number) {
        throw new Error("Method not implemented.");
    }
    updata(type: T, id: number): boolean {
        throw new Error("Method not implemented.");
    }
    delete(id: number): boolean {
        throw new Error("Method not implemented.");
    }

}

class sqlyanggeDB<T> implements DB<T>{
    add(info: T): T {
        throw new Error("Method not implemented.");
    }
    get(id: number) {
        throw new Error("Method not implemented.");
    }
    updata(type: T, id: number): boolean {
        throw new Error("Method not implemented.");
    }
    delete(id: number): boolean {
        throw new Error("Method not implemented.");
    }

}


class use{
    name:string | undefined;
    password: string | undefined;
}

let people = new use()

people.name = '张三';

people.password = '123456';


let user = new MysqLDB<use>();// 类作为参数约束传入数据的类型

user.add(people)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐