forked from csxiaoyaojianxian/JavaScriptStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-interface.ts
More file actions
80 lines (72 loc) · 1.34 KB
/
06-interface.ts
File metadata and controls
80 lines (72 loc) · 1.34 KB
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
// 非接口实现
function printLabel( labelObj:{label:string} ) {
console.log(labelObj.label);
}
var myObj = {label:"hello"};
printLabel(myObj);
// 接口 可选属性
interface Person {
name:string;
age?:number;
}
function printPerson( p:Person ) {
console.log(p.name);
}
var people = {name:"csxiaoyao"}; // 没有age属性
printPerson(people); // csxiaoyao
// 接口 函数类型
interface SearchFunc {
(source:string,subString:string):boolean;
}
var mySearch:SearchFunc;
mySearch = function(src:string,sub:string) {
var result = src.search(sub);
if(result != -1){
return true;
}else{
return false;
}
}
// 接口 数组类型
interface StringArray{
[index:number]:string;
}
var myArray:StringArray;
myArray = ["csxiaoyao","sunshine"];
alert(myArray[1]);
// 接口 class类型
interface ClockInterface{
currentTime:Date;
setTime(d:Date);
}
class Clock implements ClockInterface{
currentTime:Date;
setTime(d:Date){
this.currentTime = d;
}
constructor(h:number, m:number){
}
}
// 接口 继承
interface Shape{
color:string;
}
interface PenStroke{
penWidth:number;
}
interface Square extends Shape,PenStroke {
sideLength: number;
}
var s = <Square>{};
s.color = "blue";
s.penWidth = 10;
s.sideLength = 10;
// 接口 混合类型
interface Counter{
interval:number;
reset():void;
(srart:number):string;
}
var c:Counter;
c(10);
c.reset();