ts - 类 进阶2

简介: ES7类的使用

ES7 中类的用法

ES7 中有一些关于类的提案,TypeScript 也实现了它们,这里做一个简单的介绍。

实例属性

ES6 中实例的属性只能通过构造函数中的 this.xxx 来定义,ES7 提案中可以直接在类里面定义:

class Animal {
   
  name = 'Jack'

  constructor() {
   
  // ...
  }
}

let a = new Animal()
console.log(a.name) // Jack

静态属性

ES7 提案中,可以使用 static 定义一个静态属性:

class Animal {
   
  static num = 42

  constructor() {
   
  // ...
  }
}

console.log(Animal.num) // 42

TypeScript 中类的用法

public private 和 protected

TypeScript 可以使用三种访问修饰符(Access Modifiers),分别是 publicprivateprotected

  • public 修饰的属性或方法是公有的,可以在任何地方被访问到,默认所有的属性和方法都是 public
  • private 修饰的属性或方法是私有的,不能在声明它的类的外部访问
  • protected 修饰的属性或方法是受保护的,它和 private 类似,区别是它在子类中也是允许被访问的

下面举一些例子:

class Animal {
   
  public name
  public constructor(name) {
   
  this.name = name
  }
}

let a = new Animal('Jack')
console.log(a.name) // Jack
a.name = 'Tom'
console.log(a.name) // Tom

上面的例子中,name 被设置为了 public,所以直接访问实例的 name 属性是允许的。

很多时候,我们希望有的属性是无法直接存取的,这时候就可以用 private 了:

class Animal {
  private name
  public constructor(name) {
  this.name = name
  }
}

let a = new Animal('Jack')
console.log(a.name) // Jack
a.name = 'Tom'

// Property 'name' is private and only accessible within class 'Animal'.
// Property 'name' is private and only accessible within class 'Animal'.

需要注意的是,TypeScript 编译之后的代码中,并没有限制 private 属性在外部的可访问性。

上面的例子编译后的代码是:

var Animal = (function () {
  function Animal(name) {
  this.name = name
  }
  return Animal
}())
var a = new Animal('Jack')
console.log(a.name)
a.name = 'Tom'

使用 private 修饰的属性或方法,在子类中也是不允许访问的:

class Animal {
  private name
  public constructor(name) {
  this.name = name
  }
}

class Cat extends Animal {
  constructor(name) {
  super(name)
  console.log(this.name)
  }
}

// Property 'name' is private and only accessible within class 'Animal'.

而如果是用 protected 修饰,则允许在子类中访问:

class Animal {
  protected name
  public constructor(name) {
  this.name = name
  }
}

class Cat extends Animal {
  constructor(name) {
  super(name)
  console.log(this.name)
  }
}

当构造函数修饰为 private 时,该类不允许被继承或者实例化:

class Animal {
  public name
  private constructor (name) {
  this.name = name
  }
}
class Cat extends Animal {
  constructor (name) {
  super(name)
  }
}

let a = new Animal('Jack')

// Cannot extend a class 'Animal'. Class constructor is marked as private.
// Constructor of class 'Animal' is private and only accessible within the class declaration.

当构造函数修饰为 protected 时,该类只允许被继承:

class Animal {
  public name
  protected constructor (name) {
  this.name = name
  }
}
class Cat extends Animal {
  constructor (name) {
  super(name)
  }
}

let a = new Animal('Jack')

// Constructor of class 'Animal' is protected and only accessible within the class declaration.

修饰符还可以使用在构造函数参数中,等同于类中定义该属性,使代码更简洁。

class Animal {
  // public name: string
  public constructor (public name) {
  this.name = name
  }
}

readonly

只读属性关键字,只允许出现在属性声明或索引签名中。

class Animal {
  readonly name
  public constructor(name) {
  this.name = name
  }
}

let a = new Animal('Jack')
console.log(a.name) // Jack
a.name = 'Tom'

// Cannot assign to 'name' because it is a read-only property.

注意如果 readonly 和其他访问修饰符同时存在的话,需要写在其后面。

class Animal {
  // public readonly name
  public constructor(public readonly name) {
  this.name = name
  }
}

抽象类

abstract 用于定义抽象类和其中的抽象方法。

什么是抽象类?

首先,抽象类是不允许被实例化的:

abstract class Animal {
  public name
  public constructor(name) {
  this.name = name
  }
  public abstract sayHi()
}

let a = new Animal('Jack')

// Cannot create an instance of an abstract class.

上面的例子中,我们定义了一个抽象类 Animal,并且定义了一个抽象方法 sayHi。在实例化抽象类的时候报错了。

其次,抽象类中的抽象方法必须被子类实现:

// abstract.ts

// abstract class Animal {
   
//   constructor(public name) {
   
//     this.name = name
//   }
//   public abstract sayHi()
// }

// // 一旦类继承了抽象类,那么就必须实现你的抽象的方法
// class Cat extends Animal {
   
//   public eat() {
   
//     console.log(`${this.name} is eating.`)
//   }
// }

// let cat = new Cat('Tom')

// // Non-abstract class 'Cat' does not implement inherited abstract member 'sayHi' from class 'Animal'.

class App extends React.Component {
   
  render () {
   }
}

上面的例子中,我们定义了一个类 继承了抽象类 ``,但是没有实现抽象方法 ,所以编译报错了。

下面是一个正确使用抽象类的例子:

abstract class Animal {
   
  public name
  public constructor(name) {
   
  this.name = name
  }
  public abstract sayHi()
}

class Cat extends Animal {
   
  public sayHi() {
   
  console.log(`Meow, My name is ${
     this.name}`)
  }
}

let cat = new Cat('Tom')

上面的例子中,我们实现了抽象方法 sayHi,编译通过了。

需要注意的是,即使是抽象方法,TypeScript 的编译结果中,仍然会存在这个类,上面的代码的编译结果是:

var __extends = (this && this.__extends) || function (d, b) {
   
  for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]
  function __() {
    this.constructor = d }
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __())
}
var Animal = (function () {
   
  function Animal(name) {
   
  this.name = name
  }
  return Animal
}())
var Cat = (function (_super) {
   
  __extends(Cat, _super)
  function Cat() {
   
  _super.apply(this, arguments)
  }
  Cat.prototype.sayHi = function () {
   
  console.log('Meow, My name is ' + this.name)
  }
  return Cat
}(Animal))
var cat = new Cat('Tom')

类的类型

给类加上 TypeScript 的类型很简单,与接口类似:

class Animal {
  name: string
  constructor(name: string) {
  this.name = name
  }
  sayHi(): string {
  return `My name is ${this.name}`
  }
}

let a: Animal = new Animal('Jack')
console.log(a.sayHi()) // My name is Jack

类与接口-了解

之前学习过,接口(Interfaces)可以用于对「对象的形状(Shape)」进行描述。

这一章主要介绍接口的另一个用途,对类的一部分行为进行抽象。

类实现接口

实现(implements)是面向对象中的一个重要概念。一般来讲,一个类只能继承自另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口(interfaces),用 implements 关键字来实现。这个特性大大提高了面向对象的灵活性。

举例来说,门是一个类,防盗门是门的子类。如果防盗门有一个报警器的功能,我们可以简单的给防盗门添加一个报警方法。这时候如果有另一个类,车,也有报警器的功能,就可以考虑把报警器提取出来,作为一个接口,防盗门和车都去实现它:

// implements.ts

interface IAlarm {
   
  alert () 
}

class Door {
   

}

class SecurityDoor extends Door implements IAlarm {
   
  alert () {
   
    console.log('SecurityDoor alert')
  }
}

class Car implements IAlarm {
    
  alert () {
   
    console.log('car alert')
  }
}

一个类可以实现多个接口:

// implements1.ts

interface IAlarm1 {
   
  alert () 
}

interface ILight {
   
  on() // 灯开
  off() // 灯关
}

class Door1 {
   

}

class SecurityDoor1 extends Door1 implements IAlarm1 {
   
  alert () {
   
    console.log('SecurityDoor alert')
  }
}

class Car1 implements IAlarm1, ILight {
    
  alert () {
   
    console.log('car alert')
  }
  on () {
   
    console.log('开灯')
  }
  off () {
   
    console.log('关灯')
  }
}

上例中,Car 实现了 AlarmLight 接口,既能报警,也能开关车灯。

接口继承接口

接口与接口之间可以是继承关系:

//interface.ts

interface IBaseRoute {
   
  props?: any,
  beforeEnter?: any
  components?: any
  children?: any
  alias?: any
}
// 接口可以继承接口
interface IRoute extends IBaseRoute {
   
  path: string
  redirect?: string,
  name?: string
  component?: string
}
const routes: IRoute[]= [
  {
   
    path: '/',
    redirect: '/home'
  },
  {
   
    path: '/home',
    name: 'home',
    component: '首页组件',
    props: true
  }
]

// inteExtendIner.ts

interface Alarm {
   
  alert()
}

interface LightableAlarm extends Alarm {
   
  lightOn()
  lightOff()
}

class Car implements LightableAlarm {
   
  alert(){
   }
  lightOn(){
   }
  lightOff(){
   }
}

上例中,我们使用 extends 使 LightableAlarm 继承 Alarm

接口继承类

接口也可以继承类:

class Point {
  x: number
  y: number
}

interface Point3d extends Point {
  z: number
}

/**
interface Point3d {
  x: number
  y: number
  z: number
}**/
let point3d: Point3d = {x: 1, y: 2, z: 3}

混合类型

之前学习过,可以使用接口的方式来定义一个函数需要符合的形状:

// inter.ts

// 接口的首字母大写
// 一般以I开头
// 接口不是js的对象,元素之间需要使用;隔开而不是,或者是可以不用写
interface IPerson {
   
  firstName: string;
  lastName: string;
}

// function greeter1 (person: IPerson): String {
   
//   return 'hello ' + person.firstName + person.lastName
// }

const greeter1: (person: IPerson) => string = (person: IPerson): string => 'hello ' + person.firstName + person.lastName

const person1 = {
    firstName: '吴', lastName: '大勋' }

greeter1(person1)
相关文章
|
5月前
|
JavaScript
TS中的枚举是什么如何使用
TS中的枚举是什么如何使用
24 0
|
5月前
|
JavaScript
使用TS的一些基础知识
使用TS的一些基础知识
24 0
|
5月前
|
JavaScript 前端开发 编译器
ts面试题总结
ts面试题总结
75 0
|
6月前
|
JavaScript 前端开发 开发者
ts知识点
ts知识点
32 0
|
7月前
|
JavaScript 前端开发 开发工具
CocosCreator 面试题(五)TS有什么优缺点?为什么要用TS?
CocosCreator 面试题(五)TS有什么优缺点?为什么要用TS?
102 0
|
8月前
|
JavaScript 前端开发 程序员
ts - 类 进阶1
ES6 类的使用
|
8月前
|
JavaScript 前端开发
ts - 类
TypeScript支持JavaScript的新特性,比如支持基于类的面向对象编程。让我们创建一个Student类,它带有一个构造函数和一些公共字段。 注意类和接口可以一起工作。
|
9月前
|
JavaScript 前端开发
TS基础用法
TS基础用法
65 0
|
9月前
|
JavaScript 前端开发 IDE
TS知识点
TS知识点
|
11月前
TS在实际开发中的使用
TS在实际开发中的使用
57 0