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");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值