zoukankan      html  css  js  c++  java
  • typescript 入门教程四

    ts中的function和接口
    interface PrintCallback{
        //  匿名函數,返回类型为空
         (success:boolean):void
    }
    interface Person{
        //只读
        readonly first_name:string
        // 可选
        last_name?:string
        print(callback:PrintCallback):void
    }
    /**
     * 即使pCallback的类型定义为接口PrintCallback,其参数也可以为空
     * 如果pCallback写了参数,只要参数类型和函数返回类型遵从PrintCallback即可,即使参数名字不一样也是可以的
     */
    let pCallback:PrintCallback=(suc:boolean)=>{
        console.log("callback",suc)
    }
    let pe:Person={
        first_name:'tome',
        print:(callback:PrintCallback):void=>{
            callback(true)
        }
    }
    pe.print(pCallback)
    
    ![file](https://img2018.cnblogs.com/blog/1762823/201911/1762823-20191102170948393-1532895361.jpg)
    ##### 类型断言 类型断言和强制类型转化的区别就是,类型断言没有改变变量的实际类型,但是强制类型转化已经改变变量的类型,类型断言是在编译时期,强制类型转化是发生在运行时期 在ts中,类型断言是**在编译过程中**,当程序员知道该变量的类型,会通过类型断言方式告诉编译器,该变量是哪个类型,使得编译更加通畅,注意类型断言只是出现在编译过程中 ``` let x:any='i am jack' /** * 上面x可能是任何类型,编译器可能不知道x到底属于什么类型变量 * 这是通过表示把x断言成字符串类型,就是告诉编译器要把x当成字符串处理, * 这是就可以调用字符串处理函数substring了 */ let s=(x).substring(0,3) ``` --- ``` interface Person{ name:string age:number first_name:string } // 第二种断言的方式,这时候如果不加as断言,编译不通过,注意在js中,是可以在空的对象上加新的属性的,但是ts中是会编译失败的 let per={} as Person per.name='jack' per.age=12 per.first_name='tome' ``` ##### 单继承,多实现 ``` // 接口可以继承其他接口,类可以继承一个单独的类,但是可以实现多个接口,单继承,多实现 interface Person{ name:string } interface Programmer extends Person{ age:number } let p:Programmer={ name:'jack', age:12 } // 如果一个类实现一个接口,必须要实现接口中的所有的属性和方法 class P implements Person{ name:string constructor(name:string){ this.name=name } } ``` --- ``` // 如何用ts描述下面的列表结构 [ { "userId": 1, "id": 1, "title": "sunt aut facere", "body": "quia et suscipit" }, { "userId": 1, "id": 2, "title": "qui est esse", "body": "est rerum temporepossimus qui neque nisi nulla" } ] // 可以先定义一个接口 interface todoList{ userId:number id:number title:string body:string } //然后定义数组的类型为借口类型 let arr:todoList[]=[ { "userId": 1, "id": 1, "title": "sunt aut facere", "body": "quia et suscipit" }, { "userId": 1, "id": 2, "title": "qui est esse", "body": "est rerum temporepossimus qui neque nisi nulla" } ] ```
    ![file](https://img2018.cnblogs.com/blog/1762823/201911/1762823-20191102170948677-1593793803.jpg)
    ##### 抽象类:abstract - 抽象类或者接口均是不能实例化的,除非继承或者实现它的子类才可以实例化 - 继承抽象类的子类必须要实现抽象类中的抽象方法和抽象属性
    abstract class Person {
        constructor(name:string) {
            this.name=name
        }
        name:string
        display():void{
            console.log(this.name)
        }
        // 下面是抽象类区别于类的关键,定义一个抽象方法,注意抽象方法是没有方法体的
        abstract find(str:string):void
    }
    class Programmer extends Person{
        
        age:number
        constructor(name:string,age:number) {
            super(name)//super是必须调用
            this.age=age
        }
        // 子类必须要实现抽象类的抽象方法
        find(str: string): void {
            console.log(str)
        }
    }
    let p:Programmer=new Programmer('jack',22)
    
    补充:ts中如何定义一个函数类型的变量
     //第一种方法
     let d:(param:string)=>string
     d=function(pa:string):string{
        return pa
     }
     console.log(d('tome'))
    //  第二种,用接口方法
    interface Fn{
        (param:string):string
    }
    let f:Fn=(pa:string):string=>pa
    

    扫码关注公众号,有更多精彩文章等你哦

    file

  • 相关阅读:
    卷积神经网络(CNN)在句子建模上的应用
    Deep Learning for Information Retrieval
    Understanding Convolutional Neural Networks for NLP
    Language Modeling with Gated Convolutional Networks
    Beyond Globally Optimal: Focused Learning
    基于图像信息的搭配商品推荐
    阿里深度兴趣网络模型paper学习
    DNN论文分享
    用深度学习(DNN)构建推荐系统
    基于2-channel network的图片相似度判别
  • 原文地址:https://www.cnblogs.com/tangkaizhen/p/11783134.html
Copyright © 2011-2022 走看看