ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-19 12:00:20  阅读:134  来源: 互联网

标签:throw typescript -- ts number Method Error new id


模块

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)

标签:throw,typescript,--,ts,number,Method,Error,new,id
来源: https://blog.csdn.net/qq_41820930/article/details/122577396

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有