typeScript-class

來個簡單的栗子:

class Person{
        name: string;
        constructor(name:string){
            this.name = name
        }
        todo():void{
            console.log(`${this.name}.....`)
        }
    }
let xiaoming = new Person("小明")

我們聲明了一個Person的類,他有三個成員:屬性name、一個構(gòu)造函數(shù)、一個todo的方法,

我們使用 new構(gòu)造了 Person類的一個實例。 它會調(diào)用之前定義的構(gòu)造函數(shù),創(chuàng)建一個 Greeter類型的新對象,并執(zhí)行構(gòu)造函數(shù)初始化它

繼承

  class Person{
        name: string;
        constructor(name:string){
            this.name = name
        }
        todo():void{
            console.log(".....")
        }
    }
    class Age extends Person{
        age:number;
        constructor(name:string,age:number){
            super(name)
           this.age = age
        }
        getPeople():string{
            return `${this.name}年齡:${this.age}`
        }
    }
   let xiaoMing = new Age("小明",18)
   xiaoMing.todo()
   console.log(xiaoMing.getPeople())

這里Age 是個派生類,他派生自Person基類,通過extends關(guān)鍵字,派生類通常稱為 子類, 基類通常稱為 超類
Age繼承了Person,我們構(gòu)建了一個Age的實例,它能夠使用todo,getPeople兩個方法

公共,私有與受保護(hù)的修飾符

  • 公共(public)
    ts中類成員默認(rèn)是public,當(dāng)然也可以明確的將一個成員標(biāo)記成public,如下:
 class Person{
        public name: string;
        public  constructor(name:string){
            this.name = name
        }
        public todo():void{
            console.log(`${this.name}.....`)
        }
    }
  • 私有(private)
    當(dāng)成員標(biāo)記了private,聲明了這個類的外部無法訪問
 class Person{
        private name: string;
        constructor(name:string){
            this.name = name
        }
        public todo():void{
            console.log(`${this.name}.....`)
        }
    }
    let xiaoming = new Person("xiaoming")
    xiaoming.name //報錯 屬性“name”為私有屬性,只能在類“Person”中訪問

  • protected
    protected修飾符和private修飾符行為相似,但是protected在派生類中可以訪問
   //private
   class Person{
        private name: string;
        constructor(name:string){
            this.name = name
        }
        public todo():void{
            console.log(`${this.name}.....`)
        }
    }
    class Age extends Person {
        age:number;
        constructor(name:string,age:number){
            super(name)
            this.age = age
        }
        getInfor():void{
            console.log(`${this.name},${this.age}`) //報錯 屬性“name”為私有屬性,只能在類“Person”中訪問
        }
    }

// protected
class Person{
        protected name: string;
        constructor(name:string){
            this.name = name
        }
        public todo():void{
            console.log(`${this.name}.....`)
        }
    }
    class Age extends Person {
        age:number;
        constructor(name:string,age:number){
            super(name)
            this.age = age
        }
        getInfor():void{
            console.log(`${this.name},${this.age}`)
        }
    }
  let xiaoming = new Age("xiaoming",18)
   xiaoming.name //報錯 屬性“name”受保護(hù),只能在類“Person”及其子類中訪問

我們不能在 Person類外訪問name,但是我們可以在Age類內(nèi)訪問,因為Age類是派生自Person
構(gòu)造函數(shù)也可以被標(biāo)記成 protected,這就意味著這個類不能在包含他的類外實例,但是可以繼承

class Person{
        protected name: string;
        protected  constructor(name:string){
            this.name = name
        }
        public todo():void{
            console.log(`${this.name}.....`)
        }
    }
    class Age extends Person {
        age:number;
        constructor(name:string,age:number){
            super(name)
            this.age = age
        }
        getInfor():void{
            console.log(`${this.name},${this.age}`)
        }
    }
    let xiaoming = new Age("xiaoming",18)
    let xiaoming1 = new Person("xiaoming") //報錯 類“Person”的構(gòu)造函數(shù)是受保護(hù)的,僅可在類聲明中訪問

readonly只讀

只讀屬性,必須在聲明時候或者在構(gòu)造函數(shù)中初始化

class Person{
        readonly name: string;
        readonly age: number = 18;
        constructor(name:string){
            this.name = name
        }
       todo():void{
            this.name = "xiaohua" //報錯 無法分配到 "name" ,因為它是只讀屬性
        }
    }
    let xiaoming = new Person("xiaoming")

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容