typescript02

typescript02

typescript中的类

ES5中的类 构造函数

function Person() {
    this.name = 'Chen'
    this.age = 20
    this.status = () => {
        console.log('Chen在睡觉')
    }
}

// 在原型链上增加属性和方法 原型连上的属性会被多个实例共享 构造函数不会
Person.prototype.sex = '男'
Person.prototype.word = () => {
    console.log('Chen在上班')
}
const person = new Person() // 实例化
console.log(person.name)
console.log(person.age)
person.status()
console.log(person.sex)
person.word()

类中的静态方法

Person.getUserInfo = () => {
    console.log('静态方法')
}
Person.getUserInfo() // 调用静态方法   静态方法的调用不需要实例化

ES5中的继承

对象冒充继承

function Person() {
    this.name = 'Chen'
    this.age = 20
    this.status = () => {
        console.log('Chen在睡觉')
    }
}

Person.prototype.sex = '男'
Person.prototype.word = () => {
    console.log('Chen在上班')
}
const person = new Person() // 实例化
console.log(person.name)
console.log(person.age)
person.status()
console.log(person.sex)
person.word()

Person.getUserInfo = () => {
    console.log('静态')
}
Person.getUserInfo()

// 继承Persson类,原型链+对象冒充继承模式
function web() {
    Person.call(this)  // 对象冒充实现继承
}

const w = new web()
w.status() // 对象冒充可以继承构造函数中的属性和方法 但是无法继承原型链中的对象和方法
console.log(w.name) // result --- undefined

原型链继承

function web() {
}

web.prototype = new Person()
const w = new web()
console.log(w.age)
w.word()
w.status()
// 可以继承构造函数上的属性和方法,也可以继承原型链上的属性和方法

Tips:

  • 原型连继承无法在实例化的时候无法给父类传参

冒充 + 原型链继承

function Person(name, age) {
    this.name = name
    this.age = age
    this.status = () => {
        console.log(this.name + '干')
    }
}

Person.prototype.sex = '男'
Person.prototype.work = () => {
    console.log(this.name + '上班')
}

function web(name, age) {
    Person.call(this, name, age)
}

web.prototype = new Person()
const w = new web('找死')
w.stauts()

TS定义类

class person {
    name: string

    constructor(n: string) {
        this.name = n
    }

    print(): void {
        console.log('姓名' + this.name)
    }
}

const a = new person()
a.print('张三')

/*---------------------------------------------------分割线---------------------------------------------------*/

class person {
    name: string

    constructor(name: string) {
        this.name = name
    }

    getName(): string {
        return this.name
    }

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

const p = new person('张三')
console.log(p.getName())

p.setName('陈')
console.log(p.getName())

TS实现继承

extends,super

class person {
    name: string

    constructor(name: string) {
        this.name = name
    }

    run(): string {
        return `${this.name}打球`
    }
}

class children extends person {
    constructor(name: string) {
        super(name)
    }
}

const c = new children('Chen')
console.log(c.run())
class person {
    name: string

    constructor(name: string) {
        this.name = name
    }

    run(): void {
        console.log(this.name + '在打球')
    }
}

class children extends person {
    constructor(name: string) {
        super(name)
    }

    move(): void {
        console.log(this.name + '在移动')
    }

    run(): void {
        console.log(this.name + '在打球--子类')
    }
}

const c = new children('Chen')

c.run()
c.move()

TIPS:

  • 父类与子类有一个同样的方法时,运行的是子类的方法

类的修饰符

public: 公共类型,在类,子类,类外都可访问

private: 私有类型,在类中可以访问,在子类,类外都无法访问

protected: 保护类型,与private差不多,但有一点不同,protected成员在派生类(类、子类)中仍然可以访问。

public

// 默认在class前不加修饰符就是public类
class person {
    public name: string

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

    run(): string {
        return this.name
    }
}

const p = new person('chen')
console.log(p.run())
console.log(p.name) // 外部访问

class son extends person {
    constructor(name: string) {
        super(name)  // 子类访问父类的name
    }
    
    work(): string {
        return this.name
    }
}

private

class person {
    private name: string

    constructor(name: string) {
        this.name = name
    }

    run(): void {
        console.log(this.name)
    }
}

/*** *
 *  子类类外无法访问
 *  const p = new person('chen')
 *  p.run() 正常打印  name  可以在类中访问
 *  console.log(p.name)  tips: 'name' = private -> 'public' (name 为 私有  需要 变为 public)
 * ***/

protected

class person {
  protected name: string

  constructor(name: string) {
    this.name = name
  }

  run(): void {
    console.log(this.name)
  }
}

class son extends person {
  constructor(name:string) {
    super(name)
  }
  
  say(): void { console.log(this.name) }
}

const s = new son('chen')
s.say()
posted @ 2022-03-24 17:24  菜鸡的菜  阅读(44)  评论(0)    收藏  举报