zoukankan      html  css  js  c++  java
  • TypeScript----接口和泛型

    接口

    TypeScript的核心原则之一是对值所具有的结构进行类型检查。它有时被称做“鸭式辨型法”或“结构性子类型化”。在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。

    // 接口
    // TypeScript的核心原则之一是对值所具有的结构进行类型检查。
    // 在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。
    interface LabelValue {
        label: string;
    }
    
    function prientLabel(labelObj: LabelValue) {
        console.log(labelObj.label);
    }
    let myObj = {
        'label': 'hello Interface'
    };
    prientLabel(myObj);
    
    // LabelledValue 接口就好比一个名字,它代表了有一个 label 属性且类型为 string 的对象。
    // 只要传入的对象满足上述必要条件,那么它就是被允许的。
    
    // 可选属性
    // 带有可选属性的接口与普通的接口定义差不多,只是在可选属性名字定义的后面加一个 ? 符号。
    // 可选属性的好处之一是可以对可能存在的属性进行预定义,好处之二是可以捕获引用了不存在的属性时的错误。
    interface Person {
        name?: string;
        age?: number;
    }
    
    function printInfo(info: Person) {
        console.log(info);
    }
    
    let info = {
        'name': 'cd',
        'age': 23
    };
    printInfo(info); // {name: "cd", age: 23}
    let info2 = {
        'name': 'cd'
    };
    printInfo(info2); // {name: "cd"}
    
    // 函数类型
    // 接口能够描述 JavaScript 中对象拥有的各种各样的外形。 除了描述带有属性的普通对象外,接口也可以描述函数类型。
    // 定义的函数类型接口就像是一个只有参数列表和返回值类型的函数定义。参数列表里的每个参数都需要名字和类型。
    // 定义后完成后,我们可以像使用其它接口一样使用这个函数类型的接口。
    interface SearchFunc {
        (source: string, subString: string): boolean;
    }
    
    let mySearch: SearchFunc;
    mySearch = function(source: string,subString: string) {
        return source.search(subString) !== -1;
    };
    
    console.log(mySearch('路飞', '')); // true
    console.log(mySearch('路飞', '')); // false
    
    // 可索引类型
    // 与使用接口描述函数类型差不多,我们也可以描述那些能够“通过索引得到”的类型,比如 a[10] 或 ageMap['daniel']。
    // 可索引类型具有一个索引签名,它描述了对象索引的类型,还有相应的索引返回值类型。
    interface StringArray{
        [index: number]: string;
    }
    
    let MyArray: StringArray;
    MyArray = ['', '' , '' , ''];
    console.log(MyArray[2]); //// 类类型
    // 与 C# 或 Java 里接口的基本作用一样,TypeScript 也能够用它来明确的强制一个类去符合某种契约。
    // 我们可以在接口中描述一个方法,在类里实现它,如同下面的 setTime 方法一样:
    interface ClockInterface {
        currentTime: Date;
        setTime(d: Date);
    }
    
    class Clock implements ClockInterface {
        currentTime: Date;
        setTime(d: Date){
            this.currentTime = d;
        }
        constructor(h: number, m: number) {}
    }
    
    // 继承接口
    // 和类一样,接口也可以相互继承。 
    // 这让我们能够从一个接口里复制成员到另一个接口里,可以更灵活地将接口分割到可重用的模块里。
    // 一个接口可以继承多个接口,创建出多个接口的合成接口。
    interface Shape {
        color: string;
    }
    
    interface PenStroke {
        penWidth: number;
    }
    
    interface Square extends Shape, PenStroke {
        sideLength: number;
    }
    
    let s = <Square>{};
    s.color = 'blue';
    s.penWidth = 100;
    s.sideLength = 10;
    console.log(s); // {color: "blue", penWidth: 100, sideLength: 10}

    泛型

    软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性。组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型,这在创建大型系统时为你提供了十分灵活的功能。

    在像C#和Java这样的语言中,可以使用泛型来创建可重用的组件,一个组件可以支持多种类型的数据。这样用户就可以以自己的数据类型来使用组件。

    // 泛型
    // 如下代码,我们给 Hello 函数添加了类型变量 T ,T 帮助我们捕获用户传入的类型(比如:string)。
    // 我们把这个版本的 Hello 函数叫做泛型,因为它可以适用于多个类型。
    // 代码中 output 和 output2 是效果是相同的,
    // 第二种方法更加普遍,利用了类型推论 —— 即编译器会根据传入的参数自动地帮助我们确定T的类型。
    function Hello<T>(arg: T): T {
        return arg;
    }
    
    let outPut = Hello<string>('Hello Generic');
    let output2 = Hello('Hello Generic')
    
    console.log(outPut);
    console.log(output2);

    作者:longWinter666

  • 相关阅读:
    反转链表 16
    CodeForces 701A Cards
    hdu 1087 Super Jumping! Jumping! Jumping!(动态规划)
    hdu 1241 Oil Deposits(水一发,自我的DFS)
    CodeForces 703B(容斥定理)
    poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))
    ACM 马拦过河卒(动态规划)
    hdu 1005 Number Sequence
    51nod 1170 1770 数数字(数学技巧)
    hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  • 原文地址:https://www.cnblogs.com/cczlovexw/p/11199041.html
Copyright © 2011-2022 走看看