typescript 接口+類+枚舉+解構+擴展+接口+類+函數

interface Person {             //     定義一個接口
    firstName:string    
    lastName:string
}
function gretter(person:Person){      
    return " hello "+person.firstName+""+person.lastName
}
let user:Person = {       
    firstName:"yee",
    lastName:"huang",
}
console.log(gretter(user));     //    hello yeehuang
class User {                // 定義一個類
     fullName:string
     firstName:string
     lastName:string
     constructor(firstName:string,lastName:string){     // 構造器
        this.firstName=firstName
        this.lastName=lastName
        this.fullName=firstName+' '+lastName
     }
}



let user:Person = new User("yee","huang");     // new 一個類 並且賦值
enum Color {            //枚舉
    Red=1,
    Green,
    Blue
}

let ColorName:string=Color[2]
console.log(ColorName)
let input:[number,number] = [1,2];
let [first,second]=input   
// 等價於
// let first  =  input[0];
// let second =  input[1];
function f([first,second]:[number,number]){
    console.log(first);
    console.log(second);
}
f(input);

let [one,...rest]=[1,2,3,4];
console.log(one);    // 1
console.log(rest);   // 234

let o={
    a:"foo",
    b:12,
    c:"bar"
}
let {a,...c}=o;         // 結構取值
let total=c.b+c.c.length;
console.log(total);   
let first=[1,2];
let second=[3,4];
let bothPlus=[0,...first,...second,5];    // 展開 有了這個數組合並就不用for循環再放入了
console.log(bothPlus);
console.log(first);
console.log(second);
// 數組展開

let defaults={
    food:"spicy",
    price:"@10",
    ambiance:"sss"
}
let search={...defaults,food:"rich"}
console.log(search);

// 對象展開 後面的會覆蓋前面的    food:rich


function printLabel(labelObj:LabelVal){
    console.log(labelObj.label);
    //  labelObj.y=2    報錯 因爲是隻讀的
}
let myObj={size:10,label:"size 10 Object",y:1}
printLabel(myObj);

interface LabelVal{     // 創建一個接口
    label:string
    color?:string       // 可選屬性 
    readonly y:number   // 只讀屬性,不可修改
}


interface SearchFunc{    // 函數類型
    (source:string,subString:string):boolean
}
let mySearch:SearchFunc
mySearch=function(source:string,subString:string):boolean{
    let result =source.search(subString);
    return result>1
}


interface StringArray {        // 可索引類型
    [index:number]:string
}

let myArray:StringArray

myArray=["Bob","Fred"]

let myStr:string=myArray[0];
console.log(myStr);

interface Shape {
    color:string
}
interface PenStroke {
    penWidth:number
}
interface Square extends Shape,PenStroke {    // 接口繼承
    sidelength:number
}

 

class Animal {
    name:string
    constructor(name:string){
        this.name=name
    }
    move(distance:number=0){
        console.log(`${this.name} moved ${distance}m`)
    }
}

class Snake extends Animal {
    constructor(name:string){
        super(name)                      // 調用父類的方法
    }
    move(distance:number=5){
        console.log("slithering...")
        super.move(distance);
    }
}

class Horse extends Animal {
    constructor(name:string){
        super(name);
    }
    move(distance:number=15){
        console.log("gogogog...")
        super.move(distance);
    }
}

let sam=new Snake("Sammy");
let tom:Animal=new Horse("tommy")
sam.move();      // 調用
tom.move(12);
function buildName(firstName:string,lastName?:string,...otherName:string[]):string {    // ?可選參數 必須放在必選參數後面   ...剩餘參數是個數組
    console.log(firstName,lastName,otherName);
    return firstName+""+lastName
}
buildName("1","2","23");

 

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