<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 4\. 函數 ### 4.1 函數的定義 ~~~ function hello(name:string):void { console.log('hello',name); } hello('zfpx'); ~~~ ### 4.2 函數表達式 * 定義函數類型 ~~~ type GetUsernameFunction = (x:string,y:string)=>string; let getUsername:GetUsernameFunction = function(firstName,lastName){ return firstName + lastName; } ~~~ ### 4.3 沒有返回值 ~~~ let hello2 = function (name:string):void { console.log('hello2',name); } hello('zfpx'); hello2('zfpx'); ~~~ ### 4.4 可選參數 在TS中函數的形參和實參必須一樣,不一樣就要配置可選參數,而且必須是最后一個參數 ~~~ function print(name:string,age?:number):void { console.log(name,age); } print('zfpx'); ~~~ ### 4.5 默認參數 ~~~ function ajax(url:string,method:string='GET') { console.log(url,method); } ajax('/users'); ~~~ ### 4.6 剩余參數 ~~~ function sum(...numbers:number[]) { return numbers.reduce((val,item)=>val+=item,0); } console.log(sum(1,2,3)); ~~~ ### 4.7 函數重載 * 在Java中的重載,指的是兩個或者兩個以上的同名函數,參數不一樣 * 在TypeScript中,表現為給同一個函數提供多個函數類型定義 ~~~ let obj: any={}; function attr(val: string): void; function attr(val: number): void; function attr(val:any):void { if (typeof val === 'number') { obj.age=val; } else { obj.name=val; } } attr('zfpx'); attr(9); attr(true); console.log(obj); ~~~ ~~~ //如何定義類 //Property 'name' has no initializer and is not definitely assigned //in the constructor.ts(2564) namespace a { class Person { name: string = 'zhufeng'; age: number; constructor() { this.age = 10; } } let p1 = new Person(); console.log(p1.name); console.log(p1.age); } namespace b { // 存取器 getter setter class Person { myname: string; constructor(name: string) { this.myname = name; } get name() { return this.myname; } set name(newVal: string) { this.myname = newVal.toUpperCase(); } } let p = new Person('zhufeng'); console.log(p.name);//zhufeng p.name = 'jiagou'; console.log(p.name); } namespace c { class Person { constructor(public name: string) { } } let p = new Person('zhufeng'); p.name = 'jiagou'; } //繼承 /** * 子類繼承父類后子類的實例上就擁有了父類中的屬性和方法 * 訪問修飾符 public protected private * public 自己 自己的子類 和其它類都可能訪問 * protected 受保護的 自己和自己的子類能訪問 ,其它 類不能訪問 * private 私有的 只能自己訪問,自己的子類不能訪問,其它更不行了 */ namespace d { class Person { public name: string; protected age: number; private amount: number; constructor(name: string, age: number) { this.name = name; this.age = age; this.amount = 100; } getName() { return this.name; } setName(newName: string) { this.name = newName; } } class Student extends Person { static type = 'Student' stuNo: number; constructor(name: string, age: number, stuNo: number) { super(name, age); this.stuNo = stuNo; } static getType() { return Student.type; } getStuNo() { return this.name + this.age + this.amount + this.stuNo; } setStuNo(newStuNo: number) { this.stuNo = newStuNo; } } let s = new Student('zhufeng', 10, 1); console.log(s.name); console.log(s.age); console.log(s.amount); console.log(Student.type); Student.getType(); } ~~~ ### 5.5 繼承 * 子類繼承父類后子類的實例就擁有了父類中的屬性和方法,可以增強代碼的可復用性 * 將子類公用的方法抽象出來放在父類中,自己的特殊邏輯放在子類中重寫父類的邏輯 * super可以調用父類上的方法和屬性 ~~~ class Person { name: string;//定義實例的屬性,默認省略public修飾符 age: number; constructor(name:string,age:number) {//構造函數 this.name=name; this.age=age; } getName():string { return this.name; } setName(name:string): void{ this.name=name; } } class Student extends Person{ no: number; constructor(name:string,age:number,no:number) { super(name,age); this.no=no; } getNo():number { return this.no; } } let s1=new Student('zfpx',10,1); console.log(s1); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看