• ts 之 定义类 Class基础( 继承、多态、修饰符 public、静态属性、静态属性、抽象类)


    ts 之 定义类 Class 基础 - 实操

    1:简单定义一个 class

    class Person {
      cName: string
      cAge: number
      cSex: string
    
      constructor(name: string, age: number, sex: string) {
        this.cName = name
        this.cAge = age
        this.cSex = sex
      }
      about(str: string) {
        return '我是' + this.cName + '今年' + this.cAge + '了,性别为:' + this.cSex + '之' + str
      }
    }
    const p1 = new Person('xzl', 100, '男')
    const res = p1.about('-测试')
    console.log('res', res) // res 我是xzl今年100了,性别为:男之-测试
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    接口的 简单继承

    class Animal {
      name: string
      constructor(name: string) {
        this.name = name
      }
      run(speed: string): string {
        return this.name + '-的速度为-' + speed
      }
    }
    
    class dog extends Animal {
      constructor(name: string) {
        super(name) // 继承了父类
      }
      run(speed: string): string {
        console.log('子类方法')
        return super.run(speed)
      }
      swim(str: boolean): string {
        const res = str ? '是' : '否'
        return this.name + '- 是否会游泳-' + res
      }
    }
    const p1 = new dog('二哈')
    const res = p1.run('快')
    console.log('res', res) //res 二哈-的速度为-快
    const res2 = p1.swim(true)
    console.log('res2', res2) //res2 二哈- 是否会游泳-是
    
    • 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

    在这里插入图片描述

    2:class之中的 多态

    class Animal {
      name: string
      constructor(name: string) {
        this.name = name
      }
      run(speed: string): string {
        return this.name + '-的速度为-' + speed
      }
    }
    
    class Dog extends Animal {
      constructor(name: string) {
        super(name) // 继承了父类
      }
      run(speed: string): string {
        console.log('子类方法')
        return super.run(speed)
      }
      swim(str: boolean): string {
        const res = str ? '是' : '否'
        return this.name + '- 是否会游泳-' + res
      }
    }
    
    class Cat extends Animal {
      constructor(name: string) {
        super(name) // 继承了父类
      }
      run(speed: string): string {
        return super.run(speed)
      }
    }
    
    const an1: Animal = new Animal('动物')
    console.log(an1.run('快')) //动物-的速度为-快
    
    const p1: Dog = new Dog('二哈')
    const res = p1.run('快')
    console.log('res', res) //res 二哈-的速度为-快
    const res2 = p1.swim(true)
    console.log('res2', res2) //res2 二哈- 是否会游泳-是
    
    const c1: Animal = new Cat('小猫')
    console.log(c1.run('快')) // 小猫-的速度为-快
    
    
    • 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

    在这里插入图片描述

    3:class之中的 修饰符 public等

    • public 修饰公共的 类里面、外面都可使用
    • private 修饰私有的 只能在当前类里面调用,子类无法访问
    • protected 修饰私有的 只能在当前类里面调用,但子类可以访问
    • readonly 只读类型 无法修改当前值
    class Animal {
      readonly age: number
      public name: string
      public constructor(name: string, age = 0) {
        this.name = name
        this.age = age
      }
      public run(speed: string): string {
        return this.name + '-的速度为-' + speed
      }
      private fly(speed: string): string {
        return this.name + '-是否能飞-' + speed
      }
      protected rap(speed: string): string {
        return this.name + '-是否能rap-' + speed
      }
    }
    
    class Dog extends Animal {
      constructor(name: string) {
        super(name) // 继承了父类
      }
      run(speed: string): string {
        console.log('子类方法')
        return super.run(speed)
      }
      swim(str: boolean): string {
        const res = str ? '是' : '否'
        return this.name + '- 是否会游泳-' + res
      }
    }
    
    class Cat extends Animal {
      constructor(name: string) {
        super(name) // 继承了父类
      }
      run(speed: string): string {
        return super.run(speed)
      }
    }
    
    const an1: Animal = new Animal('动物')
    console.log(an1.run('快')) //动物-的速度为-快
    // console.log(an1.fly('是')) //属性“fly”为私有属性,只能在类“Animal”中访问。
    // console.log(an1.rap('是')) //属性“rap”受保护,只能在类“Animal”及其子类中访问
    // an1.age = 100 // 无法分配到 "age" ,因为它是只读属性
    
    const p1: Dog = new Dog('二哈')
    console.log(p1.run('快')) //res 二哈-的速度为-快
    // console.log(p1.fly('否')) //属性“fly”为私有属性,只能在类“Animal”中访问。
    
    const c1: Animal = new Cat('小猫')
    console.log(c1.run('快')) // 小猫-的速度为-快
    
    • 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
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述

    4:class之中的 set与get属性

    class Person {
      firstName: string
      lastName: string
      constructor(firstName: string, lastName: string) {
        this.firstName = firstName
        this.lastName = lastName
      }
    
      // get 属性
      get fullName() {
        return this.firstName + '-' + this.lastName
      }
      // set 属性 通过拿到数据 修改 类的属性值
      set fullName(val) {
        // console.log('val', val)
        const arr = val.split('-')
        this.firstName = arr[0]
        this.lastName = arr[1]
      }
    }
    
    const p1: Person = new Person('x', 'lp')
    console.log('p1', p1.fullName) // p1 x-lp
    p1.fullName = 'xxx-pppp'
    console.log('p1', p1.fullName) // p1 p1 xxx-pppp
    
    • 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

    5:class之中的 静态属性

    class Person {
      // 静态属性 无法在 constructor 函数之中 使用
      name1:string = 'A'
      static name2:string = 'B'
    }
    
    const p1: Person = new Person()
    console.log(Person.name2) // B
    console.log(p1.name1) // A
    p1.name1 = 'AA'
    console.log(p1.name1) // AA
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5:class之中的 抽象类

    abstract class Animal {
      abstract eat(): any
      run() {
        console.log('跑得快')
      }
    }
    
    // const an1 = new Animal() // 抽象类无法实例化对象 无法创建抽象类的实例 都说用于继承
    class Dog extends Animal {
      eat() {
        console.log('吃吃吃')
      }
    }
    const dog1 = new Dog()
    console.log('dog1', dog1.eat()) // 吃吃吃
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6:通过接口限制类

    interface Person {
      run(type: boolean): boolean;
    }
    // 多个接口 则 class Man implements Person,XXX
    class Man implements Person {
      run(type: boolean): boolean {
        return true;
      }
    }
    
    let man1 = new Man();
    console.log("flag", man1.run(true)); // flag true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    富文本编辑器vue-quill-editor的使用(可上传视频、上传图片及图片的放大缩小)
    logging 彩色日志 封装类(直接使用即可)
    面试现场!月薪3w+的这些数据挖掘SQL面试题你都掌握了吗? ⛵
    linux安全配置规范
    【前台筛选】根据查询条件,实现纯前台的数据筛选
    ASUS华硕天选4笔记本FA507NU7735H_4050原装出厂Win11系统
    Linux的查找,压缩,别名,用户管理的相关命令
    Unity --- 文本输入框的使用
    个人简历内容
    通讯网关软件009——利用CommGate X2MQTT实现MQTT访问ODBC数据源
  • 原文地址:https://blog.csdn.net/weixin_47409897/article/details/126750736