类的定义
凯 5/14/2024 ts
# 1. 简单类的定义
class Person{
public eye='两只眼睛'
constructor(name,age,color){
this.name = name;
this.age = age;
this.color = color;
this.xxx = eye;
}
sleep(){
return '正在睡觉..'
}
eat(){
return '正在吃饭'
}
//静态方法
static war(){
return '开始发动战争....'
}
}
let p1 = new Person('张麻子',40,'黄皮肤')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Person{
//定义类属性的类型
public name:string
public age:number
public color:string
constructor(name:string,age:number,color:string){
this.name = name;
this.age = age;
this.color = color;
}
sleep():string{
return '正在睡觉..'
}
eat():string{
return '正在吃饭'
}
}
let p1 = new Person('张麻子',40,'黄皮肤')
console.log(p1);
console.log( p1.eat() );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23