TS的class 继承 类型约束

class修饰符

readonly 只读

private 只能类的内部使用

protected 只能类的内部和继承的子类使用

public不限制

class的super

prototype.constructor.call

class Doms {
    name:string
    constructor(name:string) {
        this.name = name
    }
    protected getName():string {
        return this.name
    }
}

class Vues extends Doms {
    constructor(name:string) {
        super(name)
    }
}

let app1 = new Vues('cqs');
app1.name
class的static

静态方法或属性 只能静态方法之间调用 或者类名调用

class Vues {
    static version() {
        return '1.0.0'
    }
    static getVersion() {
        this.version()
    }
}

Vues.version()

interface 定义类

使用关键字 implements  后面跟interface的名字多个用逗号隔开

interface VueCLs {
    option: string,
    init(): void
}

interface Vnode {
    tag: string
}

class Vue implements VueCLs,Vnode  {
    option: string
    tag: string
    constructor(option: string, tag: string) {
        this.option = option;
        this.tag = tag;
        this.init()
    }
    init(){

    }
}
clas的abstract

抽象类 只能定义不能实现 继承了抽象类就必须实现定义的抽象方法

abstract class A {
    name: string
    constructor(name: string) {
        this.name = name;
    }
    print(): string {
        return this.name
    }

    abstract getName(): string
}

class B extends A {
    constructor() {
        super('cqs')
    }
    getName(): string {
        return this.name
    }
}

let b1 = new B();
b1.print()

console.log(b1.getName());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值