简单对TS的接口和泛型做一个小demo的举例
1)功能:定义一个操作数据库的库 支持 Mysql Mssql MongoDb
2)要求1:Mysql MsSql MongoDb功能一样 都有 add update delete get方法
3)注意:约束统一的规范、以及代码重用
4)解决方案:需要约束规范所以要定义接口 ,需要代码重用所以用到泛型
1、接口:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范
2、泛型 通俗理解:泛型就是解决 类 接口 方法的复用性1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125interface DBI<T>{
add(info:T):boolean;
update(info:T,id:number):boolean;
delete(id:number):boolean;
get(id:number):any[];
}
//定义一个操作mysql数据库的类 注意:要实现泛型接口 这个类也应该是一个泛型类
class MysqlDb<T> implements DBI<T>{
constructor(){
console.log('数据库建立连接');
}
add(info: T): boolean {
console.log(info);
return true;
}
update(info: T, id: number): boolean {
throw new Error("Method not implemented.");
}
delete(id: number): boolean {
throw new Error("Method not implemented.");
}
get(id: number): any[] {
var list=[
{
title:'xxxx',
desc:'xxxxxxxxxx'
},
{
title:'xxxx',
desc:'xxxxxxxxxx'
}
]
return list;
}
}
//定义一个操作mssql数据库的类
class MsSqlDb<T> implements DBI<T>{
constructor(){
console.log('数据库建立连接');
}
add(info: T): boolean {
console.log(info);
return true;
}
update(info: T, id: number): boolean {
throw new Error("Method not implemented.");
}
delete(id: number): boolean {
throw new Error("Method not implemented.");
}
get(id: number): any[] {
var list=[
{
title:'xxxx',
desc:'xxxxxxxxxx'
},
{
title:'xxxx',
desc:'xxxxxxxxxx'
}
]
return list;
}
}
//操作用户表 定义一个User类和数据表做映射
/*
class User{
username:string | undefined;
password:string | undefined;
}
var u=new User();
u.username='张三111';
u.password='123456';
var oMysql=new MysqlDb<User>(); //类作为参数来约束数据传入的类型
oMysql.add(u);
*/
class User{
username:string | undefined;
password:string | undefined;
}
var u=new User();
u.username='张三2222';
u.password='123456';
var oMssql=new MsSqlDb<User>();
oMssql.add(u);
//获取User表 ID=4的数据
var data=oMssql.get(4);
console.log(data);
也可对上述代码实现模块化封装
通过export将俩个泛型类暴露出去
1)module目录下db.ts1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
interface DBI<T>{
add(info:T):boolean;
update(info:T,id:number):boolean;
delete(id:number):boolean;
get(id:number):any[];
}
//定义一个操作mysql数据库的类 注意:要实现泛型接口 这个类也应该是一个泛型类
export class MysqlDb<T> implements DBI<T>{
constructor(){
console.log('数据库建立连接');
}
add(info: T): boolean {
console.log(info);
return true;
}
update(info: T, id: number): boolean {
throw new Error("Method not implemented.");
}
delete(id: number): boolean {
throw new Error("Method not implemented.");
}
get(id: number): any[] {
var list=[
{
title:'xxxx',
desc:'xxxxxxxxxx'
},
{
title:'xxxx',
desc:'xxxxxxxxxx'
}
]
return list;
}
}
//定义一个操作mssql数据库的类
export class MsSqlDb<T> implements DBI<T>{
constructor(){
console.log('数据库建立连接');
}
add(info: T): boolean {
console.log(info);
return true;
}
update(info: T, id: number): boolean {
throw new Error("Method not implemented.");
}
delete(id: number): boolean {
throw new Error("Method not implemented.");
}
get(id: number): any[] {
var list=[
{
title:'xxxx',
desc:'xxxxxxxxxx'
},
{
title:'xxxx',
desc:'xxxxxxxxxx'
}
]
return list;
}
}
2)model目录下的俩个文件
通过import来引入暴露的俩个泛型类
1:article.js文件下1
2
3
4
5
6
7
8
9
10
11
12
13import {MsSqlDb} from '../modules/db';
//定义数据库的映射
class ArticleClass{
title:string | undefined;
desc:string | undefined;
}
var ArticleModel=new MsSqlDb<ArticleClass>();
export {
ArticleClass,ArticleModel
}
2:user.ts文件下1
2
3
4
5
6
7
8
9
10
11
12
13import {MySqlDb} from '../modules/db';
//定义数据库的映射
class UserClass{
username:string | undefined;
password:string | undefined;
}
var UserModel=new MySqlDb<UserClass>();
export {
UserClass,UserModel
}
3)外面的index.ts文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import {UserClass,UserModel} from './model/user';
import {ArticleClass,ArticleModel} from './model/article';
//增加数据
var u=new UserClass();
u.username='张三';
u.password='12345655654757';
UserModel.add(u);
//获取user表数据
var res=UserModel.get(123);
console.log(res);
//获取文章表的数据
var aRes=ArticleModel.get(1);
console.log(aRes);