TypeScript 类_继承_多态_修饰符_存取器_静态成员_抽象类

ts中类

(() => {
  // ts中类的定义及使用
  class Person {
    // 定义属性
    name: string
    age: number
    gender: string
    // 定义构造函数 初始化属性的值
    constructor(name: string, age: number, gender: string) {
      this.name = name
      this.age = age
      this.gender = gender
    }
    // 定义实例方法
    say(msg: string) {
      console.log(`${msg}我是${this.name},我今年${this.age}岁,是个${this.gender}孩儿`)
    }
  }
  
  let person = new Person('小明',13,'男')
  person.say('大家好!') // 大家好!我是小明,我今年13岁,是个男孩儿
})()
类的继承
(() => {
  // 定义一个类 作为父类 超累 基类
  class Person {
    // 定义属性
    name: string
    age: number
    gender: string
    // 定义构造函数
    constructor(name: string, age: number, gender: string) {
      this.name = name
      this.age = age
      this.gender = gender
    }
    // 定义实例方法
    say(msg: string) {
      console.log(`${msg}我是${this.name},我今年${this.age}岁,是个${this.gender}孩儿`)
    }
  }

  // 定义一个子类,继承Person
  class Student extends Person { 
    constructor(name: string, age: number, gender: string) { 
      // 调用父类中的构造函数
      super(name, age, gender)
    }
    
    say() {
      // 子类中调用父类中的方法
      super.say('子类')
    }
  }

  // 实例化父类
  let person = new Person('父类名字',18,'男')
  person.say('父类')
  // 实例化子类
  let student = new Student('子类名字',10,'男')
  student.say()

})()
多态
(() => {
  // 定义一个父类
  class Animal {
    name: string
    constructor(name: string) {
      this.name = name
    }
    run() {
      console.log(`${this.name}这是父类`)
    }
  }

  // 定义子类
  class Rabbit {
    name: string
    constructor(name: string) {
      this.name = name
    }
    run() {
      console.log(`${this.name}这是子类Rabbit`)
    }
  }

  class Pig {
    name: string
    constructor(name: string) {
      this.name = name
    }
    run() {
      console.log(`${this.name}这是子类Pig`)
    }
  }
  //父类类型 可以创建子类对象
  let animal: Animal = new Animal('父类')
  let rabbit: Animal = new Rabbit('子类')
  let pig: Animal = new Pig('子类')
  //函数需要参数类型为父类  	
  function toRun(ani: Animal) {
    ani.run()
  }

  toRun(animal) //父类这是父类
  toRun(rabbit) //子类这是子类Rabbit
  toRun(pig) //子类这是子类Pig
})()
类中的修饰符

类中的成员的可访问性(属性 构造函数 方法)
public 默认修饰符
代表任何位置都可以访问 类中的成员

(() => {
  class Person { 
    public name: string
    public constructor(name: string) {
      this.name = name
    }
    public say() { 
      console.log('hello!!',this.name)
    }
  }
  let person = new Person('小明')
  console.log(person.name)
  person.say()
})()

private
类中的成员如果使用private修饰,那么外部无法访问成员的数据,子类中也无法访问

(() => {
  class Person { 
    private name: string
    public constructor(name: string) {
      this.name = name
    }
    public say() { 
      console.log('hello!!',this.name)
    }
  }
  let person = new Person('小明')

  console.log(person.name) //此处报错
  //Property 'name' is private and only accessible within class 'Person'
  person.say()
})()

protected
使用protected修饰符外部无法访问该类中的成员 子类中可以访问

(() => {
  class Person { 
    protected name: string
    public constructor(name: string) {
      this.name = name
    }
    public say() { 
      console.log('hello!!',this.name)
    }
  }
  let person = new Person('小明')
  console.log(person.name) // 此处报错
  // 属性“name”受保护,只能在类“Person”及其子类中访问。
  // Property 'name' is protected and only accessible within class 'Person' and its subclasses
  person.say()
  
  class Student extends Person { 
    constructor(name: string) { 
      super(name)
    }
    study() { 
      console.log('学习',this.name)
    }
  }

  let ming = new Student('小明')
  ming.study()
})()

readonlay
是一个关键字 可以为类中的属性成员修饰,使其不可在外部修改

(() => {
  class Person {
    readonly name: string
    constructor(name: string) {
      this.name = name
    }
    say() {
      // 类中的方法中也无法修改readonly修饰的属性
      // this.name = '李四'
      console.log('hello!!', this.name)
    }
  }
  let person = new Person('小明')
  console.log(person.name)
  // person.name = '王五'
  //无法分配到 "name" ,因为它是只读属性。
  //Cannot assign to 'name' because it is a read-only property
  console.log(person.name)

})()

使用修饰符 修饰构造函数中的参数

(() => {
  class Person {
    // 构造函数中的参数 一旦使用readonly修饰符修改 该参数叫做参数属性 类中添加了一个属性成员 外部也是无法修改该成员
    constructor(readonly name: string) {
      this.name = name
    }
    // 构造函数中的参数 一旦使用了 public 修饰 类中就有了该参数的属性成员 和普通public属性成员一样 
    // constructor(public name: string) {
    //   this.name = name
    // }

    // 同理 private 和 protected 修饰符 也会创建属性 带有各自修饰符的特性
  }
  let person = new Person('小明')
  console.log(person.name)
  // person.name = '王五'
  //无法分配到 "name" ,因为它是只读属性。
  //Cannot assign to 'name' because it is a read-only property
  console.log(person.name)

})()
存取器

有效的控制对象中成员的访问,通过getters和setters来操作

(() => {
  class Person {
    firstName: string
    lastName: string
    constructor(firstName: string, lastName: string) {
      this.firstName = firstName
      this.lastName = lastName
    }
    // 读取器
    get fullName() {
      return this.firstName + '-' + this.lastName
    }
    // 设置器
    set fullName(value) {
      let names = value.split('-')
      this.firstName = names[0]
      this.lastName = names[1]
    }
  }

  let person = new Person('西门', '吹雪')
  console.log(person.fullName) // 读取时 读取器 
  person.fullName = '诸葛-孔明' // 修改时 运行设置器
  console.log(person.fullName)
  console.log(person.firstName)
  console.log(person.lastName)
})()
静态成员

在类中通过static 修饰的属性或方法 称之为静态成员
静态成员在使用的时候时通过 类名. 的方法调用的

(() => {
  class Person {
    static fullName: string

    static say() {
      console.log('say函数')
    }
  }
  Person.fullName = '明明'
  console.log(Person.fullName)
  Person.say()
})()
抽象类
(() => {
  // 抽象类中包含抽象方法 包含实例方法 抽象类不能被实例化 
  // 子类实现抽象成员
  abstract class Animal {
    abstract eat()
  }

  class Rabbit extends Animal { 
    eat() {
      console.log('兔子吃')
    }
  }

  let rabbit = new Rabbit()
  rabbit.eat()
})()

抽象类和接口的区别

抽象类里面可以有方法的实现,但是接口完全都是抽象的,不存在方法的实现
子类只能继承一个抽象类,而接口可以被多个实现
抽象方法可以是public,protected,但是接口只能是public,默认的
抽象类可以有构造器,而接口不能有构造器;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

绝知芳誉亘千乡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值