TypeScript学习笔记(二)- 类、泛型

有两部分:静态部分实例部分

基本

class Greeter {
  greeting: string;
  constructor(message: string) {
    this.greeting = message;
  }
  greet() {
    return this.greeting;
  }
}
  • 类方法挂在prototype上


    TypeScript学习笔记(二)- 类、泛型_第1张图片
  • 类属性是实例属性

  • 类属性必须被初始化
    1.在constructor里
    2.声明时

class Greeter {
  name:string='dog';
}
  • 编译


    TypeScript学习笔记(二)- 类、泛型_第2张图片
    ts

    由上图可知编译时:

  1. 没有修饰符、protected、private成员挂在实例上;
  2. 方法挂在类的prototype上;
  3. static修饰符挂在类上;

继承与修饰符

TypeScript学习笔记(二)- 类、泛型_第3张图片

public:

  • ts里的类成员默认为public
  • 参数属性:可以在constructor的入参加public,此时该入参为实例属性


    TypeScript学习笔记(二)- 类、泛型_第4张图片
    public修饰入参

    TypeScript学习笔记(二)- 类、泛型_第5张图片
    实例

private

  • 不能在类外部访问,包括实例和派生类;
  • 如果private constructor则该类不可以被new和继承。

protected:

  • 与private相比,可以在派生类中访问;
  • 如果protected constructor则该类不可以被new,�但可以被继承。

readonly

  • 只读属性必须在声明时或构造函数里被初始化。

static

  • 可以通过类和派生类来访问,不能通过实例访问

抽象类

abstract class Talk {
  protected name: string = 'geo';
  protected constructor(str?: string) {
    // this.name = str;
  }
  abstract speaking(): void;
}
  • 关键字abstract
  • 抽象类作为其他派生类的基类不会被实例化
多态
  • 实现:使用同一个抽象类,由不同的派生类进行不同的实现
abstract class Animal {
  abstract makeSound(): void;
  move(): void {
    console.log('move')
  }
}

class Dog extends Animal {
  makeSound() {
    console.log('dog makeSound');
  }
}
let dog = new Dog();

class Cat extends Animal {
  makeSound() {
    console.log('cat makeSound');
  }
}
let cat = new Cat();

let animal: Array = [dog, cat];
animal.forEach(element => {
  console.log(element.makeSound());
});
链式调用在多态中应用
  • 链式调用
class Task{
  step1() {
    return this;
  }
  step2() {
    return this;
  }
}

new Task().step1().step2()
  • 多态
    用以保证父类与子类调用的连贯性
    使用场景:父类接口与子类接口相继调用
class Task{
  step1() {
    return this;
  }
  step2() {
    return this;
  }
}

class Story extends Task{
  next() {
    return this;
  }
}
new Story().next().step1().next().step2()

接口

interface Human {
  walk(): void;
}

interface Man extends Human {
  speak(): string;
}

interface Child extends Human {
  cry(): string;
}

class Boy implements Man, Child {
  cry(): string {
    return "";
  }

  walk() {
    return;
  }

  speak() {
    return "";
  }
}

  • 类实现接口的时候必须实现接口的所有定义,类可以实现接口没有声明的定义
  • 接口只能约束类的公有成员
  • 接口不能约束类的构造函数
  • 接口继承类
class Control {
  state = 1;
}

interface Port extends Control {

}

class Task implements Port {
  state: number;
}

new Task();

泛型

泛型类型

function identify(arg: T): T {
  return arg;
}
identify(['a', 'b']); // 设定T类型,调用
identify(['a', 'b']);// 自动推导出T类型,调用

// 定义类型
type Log = (value: T) => T;

// interface 1
interface Log {
  (value: T): T
}
// interface 2
interface Log {
  (value: T): T
}
// 调用
let log: Log = identify;
console.log(identify(2));

泛型类

class Log{
  run(value: T) {
    return value;
  }
}

let log1 = new Log();
log1.run(1);
  • 类有两部分:静态部分和实例部分。泛型指的是实例部分的类型。


    TypeScript学习笔记(二)- 类、泛型_第6张图片
    泛型不能用在静态部分

泛型约束

interface Length {
  length: number;
}

function logLength(params: T) {
  return params.length;
}

logLength([1]);

// 类型参数
function getProperty(obj: T, key: K): T[K] {
  return obj[key];
}

你可能感兴趣的:(TypeScript学习笔记(二)- 类、泛型)