-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.ts
61 lines (50 loc) · 1.42 KB
/
demo.ts
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
let burger: string = "hamburger",
calories: number = 300,
tasty: boolean = true;
function speak(food:string,energy:number): string{
return "The " + food + " has " + energy + " calories.";
}
/* --------------------------------------- */
interface Food{
name: string;
calories: number;
}
interface Talk{
(food: Food): string;
}
let talk: Talk = function(food: Food): string{
return "The " + food.name + " has " + food.calories + " calories.";
}
let ice_cream: Food = { name: "ice cream", calories: 200 };
let info:string = talk(ice_cream);
document.getElementsByTagName("h1")[0].innerHTML = info;
/* --------------------------------------- */
class Menu {
items: Array<string>;
pages: number;
constructor(item_list: Array<string>, total_pages: number) {
this.items = item_list;
this.pages = total_pages;
}
list(): void{
for(let i=0,len=this.items.length; i<len;i++){
console.log(this.items[i]);
}
}
}
let sundayMenu = new Menu(["pancakes","waffles","orange juice"],1);
sundayMenu.list();
// 继承
class ExtraMenu extends Menu {
constructor(item_list: Array<string>,total_pages: number){
super(item_list,total_pages);
}
list(): void{
console.log("This is a extra menu");
for(let i in this.items){
console.log(this.items[i]);
}
}
}
let eMenu = new ExtraMenu(['a','b','c'],2);
eMenu.list();