# TypeScript - 类
# 类的定义
class Person{
name:string;
age:number;
constructor(name:string,age:number){
this.name=name;
this.age=age;
}
sayHello(){
console.log("大家好,我是"+this.name);
}
}
// new Person 会创建一个对象 会调用构造函数
const p1 = new Person("孙悟空",18);
console.log(p1.name);
console.log(p1.age);
p1.sayHello();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 类的继承
class Person{
name:string = '';
age:number = 0;
hi(){
console.log('我是'+ this.name + ',我今年' + this.age + '岁了');
}
}
class Student extends Person{
// 子类中可以定义自己的属性
grade:string = '';
// 子类中可以定义自己的方法
study(){
console.log(this.name + this.grade);
};
}
class Teacher extends Person{
// 子类中可以定义自己的属性
salary:number = 0;
// 子类中可以定义自己的方法
teach(){
console.log(this.name + this.salary);
};
}
const s1 = new Student();
s1.name = '小明';
s1.age = 18;
s1.grade = '三年级';
s1.hi();
s1.study();
console.log(s1.name);
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
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
# 类的继承(进阶)
class Person{
name:string;
age:number;
constructor(name:string, age:number){
this.name = name;
this.age = age;
}
hi(){
console.log('我是'+ this.name + ',我今年' + this.age + '岁了');
}
}
class Student extends Person{
// 子类中可以定义自己的属性
grade:string;
// 子类中可以定义自己的方法
constructor(name:string, age:number, grade:string){
// super() 调用父类的构造函数
super(name, age);
this.grade = grade;
}
study(){
console.log(this.name + this.grade);
};
}
class Teacher extends Person{
// 子类中可以定义自己的属性
salary:number = 0;
// 子类中可以定义自己的方法
teach(){
console.log(this.name + this.salary);
};
}
const s1 = new Student('小明', 18, '三年级');
// s1.name = '小明';
// s1.age = 18;
// s1.grade = '三年级';
s1.hi();
s1.study();
console.log(s1.name);
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
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
# 类的多态
// 父类
class Animal{
action(){
console.log('我是Animal类的action方法');
}
}
// 子类
class Dog extends Animal{
action(){
console.log('我是Dog类的action方法');
}
}
// 子类
class Cat extends Animal{
action(){
console.log('我是Cat类的action方法');
}
}
// 多态
function showAction(animals:Animal[]){
animals.forEach(animal => {
// 调用的是子类的方法
// 因为:子类继承父类,子类可以重写父类的方法
// 重写:子类中定义了和父类同名的方法
// 多态:父类的引用指向子类的对象
// 多态的好处:可以提高代码的扩展性
animal.action();
});
}
showAction([new Dog(), new Cat()]);
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
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
# 成员的修饰符-private
class Person{
// private: 私有的,只能在类内部访问
private name:string = '';
getName(){
return this.name;
}
getNewName(newName:string){
this.name = newName;
}
}
const p1 = new Person();
// p1.name = '张三'; // 报错,因为name是私有的,只能在类内部访问
p1.getNewName('张三');
console.log(p1.getName());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 成员的修饰符-protected
// protected: 受保护的,只能在类内部和子类中访问
class Person{
protected name:string = '我是Person';
}
// 子类
class Student extends Person{
getName(){
return this.name;
}
}
const s1 = new Student();
// s1.name = '张三'; // 报错,因为name是受保护的,只能在类内部和子类中访问
console.log(s1.getName());
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
# 只读属性readonly
class Person{
// readonly: 只读的,只能在类内部访问,不能修改
// 只读属性可以在构造函数中赋值的,赋值后不能修改
// 属性本身是只读的,但是如果属性是对象,那么对象的属性是可以修改的
readonly name:string = '我是Person';
readonly friend?:Person;
constructor(name:string,friend?:Person){
this.name = name;
this.friend = friend;
}
}
const p1 = new Person('coderlzp',new Person('coderlzp2'));
// p1.name = '张三'; // 报错,因为name是只读的,只能在类内部访问,不能修改
console.log(p1.name);
console.log(p1.friend?.name);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# getters-setter
class Person{
private _name:string = '';
constructor(name:string){
this._name = name;
}
// getter: 用来读取属性的 用来获取属性的值
get name(){
return this._name;
}
// setter: 用来设置属性的 用来设置属性的值
set name(newName:string){
this._name = newName;
}
}
const p1 = new Person('coderlzp');
console.log(p1.name);
p1.name = '张三';
console.log(p1.name);
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
# 类的静态成员
// class Person{
// name:string = '我是Person';
// age:number = 18;
// }
// const p1 = new Person();
// p1.name = '张三';
// console.log(p1.name);
class Person{
// static: 静态的,可以直接通过类名访问
static time:string = '10-10-10';
static getName(){
console.log(this.time);
}
}
Person.time = '20-20-20';
Person.getName();
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
# 抽象类abstract
function makeArea(shape: Person) {
return shape.getArea();
}
// 抽象类 abstract: 用来定义抽象类和其中的抽象方法 可以被继承,但是不能被实例化 没有函数体
// 抽象类不能被实例化,只能被继承
// 抽象类中可以有抽象方法,抽象方法只能在抽象类中
abstract class Person{
abstract getArea():number;
}
// const p1 = new Person(); // 报错,因为抽象类不能被实例化
class Circle extends Person {
private r: number;
constructor(r: number) {
// 调用父类的构造函数
super()
this.r = r;
}
getArea() {
return Math.PI * this.r * this.r;
}
}
class Square extends Person {
private width: number;
private height: number;
constructor(width: number,height: number) {
super()
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
}
const c1 = new Circle(10);
const s1 = new Square(10, 20);
console.log(makeArea(c1));
console.log(makeArea(s1));
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
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