-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.ts
More file actions
24 lines (20 loc) · 902 Bytes
/
class.ts
File metadata and controls
24 lines (20 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//可以看生成的js就知道了 name,width height是constructor私有的变量 area 和color 是私有属性
class Shape {
area: number;
private color: string;// 这个位置如果是私有的private 定义类型
constructor (public name: string, width: number, height: number ) {
this.area = width * height;
this.color = "pink";
};
shoutout() {
return "I'm " + this.color + " " + this.name + " with an area of " + this.area + " cm squared.";
}
}
//构造器中参数(name, width 和 height) 的作用域是局部变量
var square = new Shape("square", 30, 30);
console.log( square.shoutout() );
console.log( 'Area of Shape: ' + square.area );
console.log( 'Name of Shape: ' + square.name );
console.log( 'Color of Shape: ' + square.color );
console.log( 'Width of Shape: ' + square.width );
console.log( 'Height of Shape: ' + square.height );