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了,性别为:男之-测试

接口的 简单继承

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 二哈- 是否会游泳-是

在这里插入图片描述

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('快')) // 小猫-的速度为-快

在这里插入图片描述

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('快')) // 小猫-的速度为-快

在这里插入图片描述

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

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

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()) // 吃吃吃

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值