es6 class类 typescript中的类,文章三

4 篇文章 0 订阅

定义一个类

class Person {
  name: string; // 属性 前面省略了public关键字,默认是public
  constructor(name: string) {
    // 构造函数,实例化触发的方法
    this.name = name
  }
  run(): void {
    console.log(this.name + '在运动')
  }

  getNane():string {
    return this.name
  }

  setName(name: string):void {
    this.name = name
  }
}

// 实例化
var p = new Person('李狗蛋')
p.run() // 李狗蛋在工作
p.getNane() // 李狗蛋
p.setName('张翠花') // name = 张翠花

继承

class Person {
  name: string // 属性 前面省略了public关键字,默认是public
  constructor(name: string) {
    // 构造函数,实例化触发的方法
    this.name = name
  }
  run(): void {
    console.log(this.name + '在运动')
  }
}

// 继承 通过关键字extends继承父类,子类必需调用super
class Student extends Person {
  constructor(name: string) {
    super(name) // 初始化父类构造函数
  }
  // 子类不仅可以继承父类的属性和方法,还可以拓展自己的属性和方法
  work(): void {
  	console.log(this.name + '在工作')
  }
  // 如果子类和父类有同样的方法,会调用子类的方法,如果调用的方法在子类找不到就去父类找,一直往上找,直到找不到为止
  run(): void {
    console.log(this.name + '在运动')
  }
}

var s = new Student('赵铁柱')
s.run() // 赵铁柱在运动
s.work() // 赵铁柱在工作

类里面的修饰符

// 类里面的修饰符,typescript里面定义属性的时候给我们提供了三种修饰符
/**
 * public     共有类型:在类里面、子类、外部都可以访问
 * protected  保护类型:在类里面、子类都可以访问。在类外部不可访问
 * private    私有类型:在类里面可以访问。在子类、外部不可访问
 * 如果属性不加修饰符,默认是public
 */

// 演示public
class Person {
  public name: string
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
}

class Student extends Person {
  constructor(name: string) {
    super(name)
  }
  log(): void {
    console.log(this.name)
  }
}

let p = new Person('李狗蛋')
console.log(p.name) // 李狗蛋

let s = new Student('张翠花')
s.log() // 张翠花


// 演示protected
class Person {
  protected name: string
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
}

class Student extends Person {
  constructor(name: string) {
    super(name)
  }
  log(): void {
    console.log(this.name)
  }
}

let p = new Person('李狗蛋')
// protected在外部调用时还是可以执行的,最终转化为js,但是在ts会语法提示报错
console.log(~~p.name~~ ) // 李狗蛋

let s = new Student('张翠花')
s.log() // 张翠花

// 演示private
class Person {
  private name: string
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
}

class Student extends Person {
  constructor(name: string) {
    super(name)
  }
  log(): void {
  // 私有只能在类里面使用,外部有子类会语法提示错误
    console.log(~~this.name~~ )
  }
}

let p = new Person('李狗蛋')
p.run() // 李狗蛋  类内部使用了name
// 私有只能在类里面使用,外部有子类会语法提示错误
console.log(~~p.name~~ ) // 李狗蛋

let s = new Student('张翠花')
s.log() // 张翠花

类里面的静态属性和方法

/**
 * 类里面的静态属性、静态方法
 * 在属性和方法前面加 static
 */
class Person {
  name: string
  static code: number = 1
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
  static print(): void {
    // 静态方法里面不能调用实例属性
    console.log('静态方法' + ~~this.name~~ ) // 语法提示错误
    // 只能调用静态属性
    console.log('静态方法' + Person.code) // 静态方法1
    console.log('静态方法')
  }
}

console.log(Person.code) // 1
Person.print() // 静态方法

多态:多态属于继承

// 类:多态,父定义一个方法不去实现,让继承它的子类去实现,每一个子类有不同的表现
class Animal {
  name: string
  constructor(name: string) {
    this.name = name
  }
  // 如果父的方法能满足子类的需求,那么子类就不用写eat方法
  // 相反父类不能满足,那么就在子类再写一个eat方法
  eat(food: string): void {
    console.log(`${this.name}${food}`)
  }
}

class Dog extends Animal {
  constructor(name: string) {
    super(name)
  }
}

// 实例
let dog = new Dog('小黑')
dog.eat('狗粮') // 小黑吃狗粮

class Cat extends Animal {
  constructor(name: string) {
    super(name)
  }
  // 父类方法不能满足子类,子类自己写一个方法
  eat(food: string): void {
    console.log(`${this.name}${food},喝水`)
  }
}

let cat = new Cat('小花猫')
cat.eat('猫粮') // 小花猫吃猫粮,喝水

typescript中的抽象类

/**
 * typescript中的抽象类,它是提供其它类继承的基类,不能直接被实例化
 * abstract关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现
 * 抽象类和抽象方法用来定义标准
 */
abstract class Animal {
  name: string
  constructor(name: string) {
    this.name = name
  }
  // 抽象方法
  abstract eat(): void
}

// 无法直接实例化,,错误的写法
// let animal = new Animal('小花猫') 错误的写法
// abstract定义标准, 是子类必须定义抽象类的抽象方法,不然语法提示错误
class Dog extends Animal {
  constructor(name: string) {
    super(name)
  }
  // 抽象类的子类,必须实现抽象类的抽象方法
  eat(): void {
    console.log(this.name + '吃东西')
  }
}

let dog = new Dog('小黑')
dog.eat() // 小黑吃东西
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值