ts中的类

134 阅读2分钟

关键字 class

定义我们的类

class Company {
    name: string = '跃码教育'
}

使用我们的类

class Company {
    name: string = '跃码教育'
}
let com = new Company()
console.log(com)//Company { name: '跃码教育' }

面向对象编程的三大特性

  • 多态
  • 继承
  • 封装

关键字extends

class Father {
    firstname: string = 'Lee'
}
class Child extends Father { }

关键字 private

class Father {
    firstname: string = 'Lee'
    private nickename: string = '李四' // 私有的 类的外面访问不到
}
let father = new Father()
// console.log(father.nickename) // 错误的 以为是私有的,只能在类内部访问
class Child extends Father { }
let child = new Child()

关键 protected

你的成员变量,不想暴露在外面,但是你要想暴露给子类

class Father {
    firstname: string = 'Lee'
    private nickename: string = '李四' // 私有的 类的外面访问不到
    protected dna: string = 'daldmfkamfoasd'
}
let father = new Father()
// console.log(father.nickename) // 错误的 以为是私有的,只能在类内部访问
// console.log(father.dna) // 错误的 类的外面也是拿不到 protected 声明的成员变量
class Child extends Father {
    getDna() {
        return this.dna + '   dakdklaa'
    }
}
let child = new Child()
let childDna = child.getDna()
console.log(childDna)//daldmfkamfoasd   dakdklaa

多态示例

interface IAnimal {
    name: string
}
interface ICat extends IAnimal {
    say(str: string): string
}
interface IDog extends IAnimal {
    say(str: string, s2: string): string
}
class Cat implements ICat {
    name: string
    say(s1: string): string {
        return s1
    }
}
// 接口重在签名,也就是约束
class Dog implements IDog {
    name: string
    say(s1: string, st2: string): string {
        return s1 + st2
    }
}
let cat: ICat = new Cat()
console.log(cat.say('cat'))
let dog: IDog = new Dog()
console.log(dog.say('dog1', 'dog2'))

关键字 readonly

class Father {
    readonly firstname: string = 'Lee'
    private _nickename: string = '李四' // 私有的 类的外面访问不到
    protected dna: string = 'daldmfkamfoasd'
}
let father = new Father()
// father.firstname = 'Le222222' readonly 只能读取,不能修改
console.log(father.firstname)

getter/setter

class Father {
    readonly firstname: string = 'Lee'
    private _nickename: string = '李四' // 私有的 类的外面访问不到
    protected dna: string = 'daldmfkamfoasd'

    get nickname(): string {
        return this._nickename
    }
    set nickname(str: string) {
        this._nickename = this._nickename + str
    }
}
let father = new Father()
// father.firstname = 'Le222222' readonly 只能读取,不能修改
console.log(father.firstname)
console.log(father.nickname)
father.nickname = '111111111111'
console.log(father.nickname)

静态成员变量 static

class Father {    static lee = 'lee'    readonly firstname: string = 'Lee'    private _nickename: string = '李四' // 私有的 类的外面访问不到    protected dna: string = 'daldmfkamfoasd'    get nickname(): string {        return this._nickename    }    set nickname(str: string) {        this._nickename = this._nickename + str    }}let father = new Father()// father.firstname = 'Le222222' readonly 只能读取,不能修改console.log(father.firstname)console.log(father.nickname)father.nickname = '111111111111'console.log(father.nickname)console.log(Father.lee)

抽象类

abstract class man {    abstract think(): void}

抽象类中的抽象方法要在子类中实现

abstract class man {    abstract think(): void}class Father extends man {    static lee = 'lee'    readonly firstname: string = 'Lee'    private _nickename: string = '李四' // 私有的 类的外面访问不到    protected dna: string = 'daldmfkamfoasd'    get nickname(): string {        return this._nickename    }    set nickname(str: string) {        this._nickename = this._nickename + str    }    think() { }}

类中的构造函数,关键字 constructor,super

abstract class man {    abstract think(): void}class Father extends man {    static lee = 'lee'    readonly firstname: string = 'Lee'    private _nickename: string = '李四' // 私有的 类的外面访问不到    protected dna: string = 'daldmfkamfoasd'    constructor() {        super()        this.firstname = 'aaaa'    }    get nickname(): string {        return this._nickename    }    set nickname(str: string) {        this._nickename = this._nickename + str    }    think() { }}