类的简写
凯 5/13/2024 ts
# 1. 类的简写
class Person{
//定义类属性的类型
// public name:string
// public age:number
// public color:string
constructor(public name:string,public age:number, public 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//电器类
//属性: power:电 greade:能耗等级 do:作用
//方法: 启动 关闭
class DQ{
constructor(public power:string, public greade:number,public doing:string){
}
public open():string{
return '启动...'
}
public close():string{
return '关闭...'
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16