[轉]TypeScript編寫類繼承函數相關的代碼

TypeScript編寫類,繼承、函數相關的代碼

class Person {
    private name:string
    private age:Number

    constructor(name:string, age:Number) {
        this.name = name;
        this.age = age
    }

    public getPersonInfo():string{
        return `My name is ${this.name} ange age is ${this.age}`;
    }
}


class Employee extends Person {
    private department:string
    constructor(name:string, age:number, department:string) {
        super(name, age)
        this.department = department
    }

    public getEmployeeInfo():string {{
        return this.getPersonInfo() + ` and work in ${this.department}`
    }}
}


let person2 = new Employee("baoshan", 33, "Huawei")
console.log(person2.getPersonInfo())
console.log(person2.getEmployeeInfo())

 

 

參考官方文檔:<HarmonyOS第一課>ArkTS開發語言介紹-華爲開發者學堂 (huawei.com)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章