类的成员修饰符

在TypeScript中,类的属性和方法支持三种修饰符:

public、private、protected

  • public 修饰的是在任何地方可见、公有的属性或方法,默认编写的属性就是public的
  • private 修饰的是仅在同一类中可见、私有的属性或方法
  • protected 修饰的是仅在类自身及子类中可见、受保护的属性或方法
  • public是默认的修饰符,也是可以直接访问的
  1. class Person{
  2. // 默认为public
  3. name:string = 'xxx'
  4. }
  5. let p = new Person
  6. console.log(p.name); //xxx
  7. class Person2 {
  8. // private 类中才可以访问到
  9. private name: string = 'xxx';
  10. getname() {
  11. return this.name;
  12. }
  13. }
  14. let p2 = new Person2();
  15. // console.log(p.name); //私有属性,类中才可以访问到
  16. console.log(p2.getname()); //xxx
  17. class Person1 {
  18. // protected 受保护 类中及子类在才可以访问
  19. protected name1: string = 'xxx';
  20. getname() {
  21. return this.name1;
  22. }
  23. }
  24. let p1 = new Person1();
  25. // console.log(p1.name); //受保护 类中和子类在才可以访问
  26. console.log(p1.getname());//xxx
  27. class Stu extends Person1 {
  28. get() {
  29. return this.name1;
  30. }
  31. }
  32. let s1 = new Stu();
  33. console.log(s1.get()); //xxx

只读属性readonly

如果有一个属性我们不希望外界可以任意的修改,只希望确定值后直接使用,那么可以使用readonly

  1. class Person {
  2. readonly name: string = 'yy';
  3. age: number = 20;
  4. readonly friend?: Person;
  5. //只读属性可以在constructor中修改
  6. constructor(name: string, friend?: Person) {
  7. this.name = name;
  8. this.friend = friend;
  9. }
  10. }
  11. let p = new Person('xxxx');
  12. console.log(p.name); //xxxx
  13. // p.name = 'mmmm' // 报错,因为只读不能修改
  14. let p2 = new Person('nnn', new Person('kkk'));
  15. // p2.friend = {'c'} // 报错,因为只读不能修改
  16. // 可以这样修改
  17. console.log(p2.friend);
  18. if(p2.friend){
  19. p2.friend.age = 30;
  20. console.log(p2.friend.age); //30
  21. }
  22. export {};

getters/setters

前面一些私有属性我们是不能直接访问的,或者某些属性我们想要监听它的获取(getter)和设置(setter)的过程, 这个时候我们可以使用存取器

  1. class Person {
  2. private _name: string = ''
  3. set name(newName) {
  4. this._name = newName;
  5. }
  6. get name() {
  7. return this._name;
  8. }
  9. }
  10. let p = new Person();
  11. p.name = 'ccc';
  12. console.log(p.name); //ccc
  13. export {};

静态成员

前面我们在类中定义的成员和方法都属于对象级别的, 在开发中, 我们有时候也需要定义类级别的成员和方法

在TypeScript中通过关键字static来定义:

  1. class Person {
  2. static time: string = '10:10';
  3. static get() {
  4. return this.time;
  5. }
  6. }
  7. console.log(Person.time); //10:10
  8. console.log(Person.get()); //10:10
  9. export {};

抽象类abstract

继承是多态使用的前提。

所以在定义很多通用的调用接口时, 我们通常会让调用者传入父类,通过多态来实现更加灵活的调用方式

但是,父类本身可能并不需要对某些方法进行具体的实现,所以父类中定义的方法,,我们可以定义为抽象方法

什么是抽象方法?

  • 在TypeScript中没有具体实现的方法(没有方法体),就是抽象方法。
  • 抽象方法,必须存在于抽象类中
  • 抽象类是使用abstract声明的类
  • 抽象类有如下的特点:

    • 抽象类是不能被实例的话(也就是不能通过new创建)
    • 抽象方法必须被子类实现,否则该类必须是一个抽象类
  1. function makeArea(shape: Shape) {
  2. return shape.getArea();
  3. }
  4. abstract class Shape {
  5. abstract getArea()
  6. }
  7. class Jx extends Shape {
  8. width: number;
  9. length: number;
  10. constructor(width: number, length: number) {
  11. super();
  12. this.width = width;
  13. this.length = length;
  14. }
  15. getArea(){
  16. return this.width * this.length
  17. }
  18. }
  19. let jx = new Jx(10,20)
  20. console.log(makeArea(jx)); //200

类的类型

类本身也是可以作为一种数据类型的

  1. class Person {
  2. name: string;
  3. eat() {}
  4. }
  5. //根据类型推导 p的类型为Person
  6. let p = new Person();
  7. let p1: Person = {
  8. name: 'xxx',
  9. eat: function () {},
  10. };