TypeScript 类(class)与修饰符的详细使用

一、简介

  • 通过 class 关键字定义一个类,然后通过 new 关键字可以方便的生产一个类的实例对象,这个生产对象的过程叫 实例化,类的成员就是类中所有的属性和方法。

    // 定义类
    class Person {
      // 名称
      name: string
      // 年龄
      age: number
      // 构造函数
      constructor(name: string, age: number) {
        // 更新熟悉数据
        this.name = name
        this.age = age
      }
    }
    
    // 发送一个人的信息
    function sendPerson (person: Person) {
      console.log(`姓名:${person.name},年龄:${person.age}`)
    }
    
    // 实例化对象
    const p = new Person('dzm', 20)
    sendPerson(p) // 名称:dzm,年龄:20
    
  • 实例在new出来的时候,它实际是调用了类中的一个方法进行初始化,这个方法被叫做构造器,一般都会在类中显示地写上 constructor 方法,如果没有显示定义的 constructor,就会调用系统自带的 constructor 构造函数。

二、成员修饰符

  • 访问修饰符的作用就是用于限制别人乱用类中的东西

  • 访问修饰符

    • public:公开的,谁都能用(默认就是 public

    • private:私有的,仅类自己能使用,子类与外部都不能使用

    • protected:受保护的,仅类和类的子类能使用,外部不能使用

    // 定义类
    class Person {
      // 公开参数
      name: string
      // 公开参数
      public age: number
      // 私有参数
      private num:number = 10
      // 内部参数
      protected num1: number = 20
      // 构造函数
      constructor(name: string, age: number) {
        // 更新熟悉数据
        this.name = name
        this.age = age
      }
      // 发送个人信息
      public send() {
        console.log('发送成功1')
      }
      // 私有方法
      private post() {
        console.log('发送成功2')
      }
    }
    
  • 只读修饰符

    • readonly:只能读不能写
    class Person {
      // 声明赋值
      readonly name = 'dzm'
    }
    let p = new Person()
    console.log(p.name)
    // 不能赋值
    // p.name = 'xxx'
    

    readonly 只能在 constructor 构造方法初始化时赋值,或者声明时赋值,之后都不能在修改值。

    class Person{
      readonly name: string
      // 构造初始化赋值
      constructor(name: string) {
        this.name =  name
      }
    }
    
  • 静态修饰符

    • static:静态成员无需实例化,直接通过类名调用
    // 定义类
    class Person {
      // 公开参数
      name: string
      // 公开参数
      public age: number
      // 静态参数
      static num:number = 10
      // 构造函数
      constructor(name: string, age: number) {
        // 更新熟悉数据
        this.name = name
        this.age = age
      }
      // 发送个人信息
      static send() {
        console.log('发送成功')
      }
    }
    
    // 不需要实例化对象,通过类名就能进行访问
    console.log(Person.num)
    Person.send()
    
  • 总结

    1、上面总共分为三种类型修饰符:访问修饰符只读修饰符静态修饰符

    2、修饰符是可选的,在没有写任何修饰符的情况下,默认为 public

    3、同类型修饰符只能有一个,也就是上面 三种修饰符 可以组合起来修饰一个成员。

    4、三种类型修饰符有先后顺序,分别是:访问静态只读,即:public/static/protectedstaticreadonly

三、实现(implements)

  • 类可以被多个接口协议约束,类也可以作为接口使用,也就是 implements 后面可以添加 单个或多个 接口与类

    格式:class 类名 implements 接口名, 接口名, 类名 ... {}

    例如:class Person implements Action, Info {}

  • 案例

    // 定义行动接口
    interface Action {
      // 跑起来
      run():void
    }
    
    // 定义信息接口
    interface Info {
      // 用户名称
      name: string
    }
    
    // 定义一个类,并实现上面的接口
    class Person implements Action, Info {
      // 用户名称
      name: string
      // 跑起来
      run(): void {
        console.log(`${this.name} 跑起来了`)
      }
    }
    
    // 定义一个类,并实现上面的类接口
    class Person2 implements Person {
      name: string
      run(): void {
        console.log(`${this.name} 跑起来了`)
      }
    }
    
    // 实例化
    const p = new Person()
    p.name = 'DZM'
    p.run()
    
    // 实例化
    const p2 = new Person2()
    p2.name = 'XYQ'
    p2.run()
    

四、继承(extends)

  • 类不能继承接口协议(interface),只能通过 implements 关键词进行实现,支持实现多个接口协议。

  • 类不支持多继承,也就是 extends 后面只能存在一个父类,但是可以通过多个接口协议来实现多继承。

    格式:class 类名 extends 类名 implements 接口名, 接口名, 类名 ... {}

    例如:class Person3 extends Person implements Action, Info {}

  • 接口支持多继承。

    格式:interface 接口名 extends 接口名, 接口名, 类名 ... {}

    例如:interface Person extends Action, Info {}

  • 案例

    // 定义行动接口
    interface Action {
      // 跑起来
      run():void
    }
    
    // 定义信息接口
    interface Info {
      // 用户名称
      name: string
    }
    
    // 定义一个类,并实现上面的接口
    class Person implements Action, Info {
      // 用户名称
      name: string
      // 跑起来
      run(): void {
        console.log(`${this.name} 跑起来了`)
      }
    }
    
    // 定义一个类,并实现上面的类接口
    class Person2 extends Person {
      name: string
      run(): void {
        console.log(`${this.name} 跑起来了`)
      }
    }
    
    // 定义一个类,并实现上面的类接口
    class Person3 extends Person implements Action, Info {
      name: string
      run(): void {
        console.log(`${this.name} 跑起来了`)
      }
    }
    
    // 实例化
    const p = new Person()
    p.name = 'DZM'
    p.run()
    
    // 实例化
    const p2 = new Person2()
    p2.name = 'XYQ'
    p2.run()
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
TypeScript修饰符有以下几种: 1. public public是默认修饰符,即不写修饰符时默认为public。public修饰的属性或方法可以在的内部和外部使用。 2. private private修饰的属性或方法只能在的内部使用,无法在的外部使用。 3. protected protected修饰的属性或方法可以在的内部和子使用,但无法在的外部使用。 4. readonly readonly修饰的属性只能在初始化时或构造函数中赋值,之后就无法再修改。 示例代码: ```typescript class Person { public name: string; // 公共属性 private age: number; // 私有属性 protected gender: string; // 保护属性 readonly id: number; // 只读属性 constructor(name: string, age: number, gender: string, id: number) { this.name = name; this.age = age; this.gender = gender; this.id = id; } public sayHello() { // 公共方法 console.log(`Hello, my name is ${this.name}.`); } private getAge() { // 私有方法 return this.age; } protected getGender() { // 保护方法 return this.gender; } } class Student extends Person { public getGenderAndAge() { console.log(`My gender is ${this.getGender()} and age is ${this.getAge()}.`); } } const person = new Person('Tom', 18, 'male', 1001); console.log(person.name); // Tom // console.log(person.age); // 无法访问 // console.log(person.gender); // 无法访问 // person.id = 1002; // 无法修改 person.sayHello(); // Hello, my name is Tom. const student = new Student('Jerry', 17, 'female', 1002); // console.log(student.age); // 无法访问 // console.log(student.gender); // 无法访问 console.log(student.name); // Jerry console.log(student.id); // 1002 student.getGenderAndAge(); // My gender is female and age is undefined. ``` 在上面的示例代码中,我们定义了一个Person,其中包含了公共属性、私有属性、保护属性、只读属性、公共方法、私有方法和保护方法。这些属性和方法都有不同的修饰符,可以在不同的场景下使用。同时,我们还定义了一个Student,继承自Person,可以访问Person中的保护属性和保护方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡尔特斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值