typescript 接口学习-接口继承与实现

export { };
// usb接口类型
type usbInterFaceType = "Type-C" | "Lightning" | "Micro USB";

// 数据线运行状态
enum DataLineWorkStatus { IN_SERVICE, LEAVE_UNUSED };

// 数据线状态,正常,部分异常,部分损坏,完全不可用
enum DataLineStatus { NORMAL, PARTIAL_ABNORMALITY, PARTIAL_DAMAGE, COMPLETELY_UNAVAILABLE };

// 数据线能力,充电,数据传输,扩展功能
interface IDataLineFunc {
    hasCharge: boolean;
    hasDataTransmission?: boolean;
    hasExtend?: boolean;
}
// 手机数据线接口
interface IDataLine extends IDataLineFunc {
    dataLineType: usbInterFaceType;
    status?: DataLineStatus;
    runStatus?: DataLineWorkStatus;
    onCharge?(batteryLevel: number): any;
    onDataTransmission?(): any;
    durationUsingTime?: number;
    isConnectComputer?(): void;
}

class DataLine implements IDataLine {
    public dataLineType;
    public status;
    public runStatus: DataLineWorkStatus;
    public hasCharge;
    public hasDataTransmission?;
    public hasExtend?;
    constructor(options: IDataLine) {
        this.dataLineType = (options as IDataLine).dataLineType;
        this.hasCharge = options.hasCharge || true;
        this.hasDataTransmission = options.hasDataTransmission || false;
        this.hasExtend = options.hasExtend || false;
        this.status = options.status || DataLineStatus.NORMAL;
        this.runStatus = options.runStatus || DataLineWorkStatus.LEAVE_UNUSED
    }
    // 监听充电,所有操作保证电源接通情况下的监测
    onCharge(batteryLevel: number): any {
        if (batteryLevel === 100) {
            return console.info('当前手机电池电量充足,请移除电源数据线');
        }
        // 先判断数据线是否正常
        if (this.status === DataLineStatus.COMPLETELY_UNAVAILABLE) {
            return new Error('当前数据线完全不可用,请更换数据线');
        }
        // 数据线部分损坏,用户自查是否充电正常
        if (this.status === DataLineStatus.PARTIAL_DAMAGE) {
            return console.info('数据线部分损坏,用户自查是否充电正常');
        }
        // 数据线部分异常,用户自查是否充电正常
        if (this.status === DataLineStatus.PARTIAL_ABNORMALITY) {
            return console.info('数据线部分异常,用户自查是否充电正常');
        }
        // 如果正常,充电异常,请确认是否数据线连接状态ok  
        if (this.status === DataLineStatus.NORMAL) {
            this.runStatus = DataLineWorkStatus.IN_SERVICE;
            return console.info('当前运行状态良好,持续充电中');
        }
    }
}




你可能感兴趣的:(typescript,学习,前端)