TypeScript类

前言

感觉上TS的类和C#用起来差不多,挺顺手

DEMO

class PC {

    // 字段 共有(默認) 保護 私有
    public GPU1: string;
    protected GPU2: string;
    private CPU: string;

    // 构造函数 
    constructor(name: string) {
        this.CPU = name;
    }

    // 方法 
    disp(): void {
        
        console.log("GPU1为 :" + this.GPU1);
        console.log("GPU2为 :" + this.GPU2);
        console.log("CPU为 :" + this.CPU);
    }
}


let myPC = new PC('I5-3470');
myPC.GPU1 = 'GTX3080';
myPC.disp();
posted @ 2021-03-01 13:34  sorachannel  阅读(57)  评论(0)    收藏  举报