中文网:https://www.tslang.cn/

官网:http://www.typescriptlang.org/

目录:

Typescript 中的接口

接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用。接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据,也不关心这些类里方法的实现细节,它只规定这批类里必须提供某些方法,提供这些方法的类就可以满足实际需要。

Typescrip 中的接口类似于 java,同时还增加了更灵活的接口类型,包括属性、函数、可索引和类等。

定义标准。

属性类接口

对 对象属性 的约束。

ts 中属性对象传参,对方法传入的对象及属性进行约束。

function printLabel (label: {label:string}):void {
console.log(`print label:${label.label}`);
}
printLabel({label: '标签'}); // 必须传入带有label属性,且属性值是字符串的对象
// printLabel({label: '标签', name: 'text'}); // 错误
// printLabel({name: '标签'}); // 错误
// printLabel('标签'); // 错误

接口:行为和动作的规范,对批量方法传入参数进行约束。

用关键字interface定义,用分号结束。约定接口名首字母大写。

interface FullName {
firstName:string; // 注意,用分号结束
secondName:string;
} function printName (name:FullName):void {
// 必须传入对象,且带有属性:firstName,secondName,且属性值都是字符串
console.log(`${name.firstName} -- ${name.secondName}`);
}
printName({firstName:'张', secondName: '三'}); // 必须带有 firstName,secondName
// printName({firstName:'张', secondName: '三', name: '张三'}); // 错误 只能带有 firstName,secondName const nameObj = {firstName:'张', secondName: '三', name: '张三', age: 20};
printName(nameObj); // 正确,这种方式,可以传额外的,但是 firstName、secondName 必须有 printName({ // 参数的顺序可以不一样
secondName: '四',
firstName: '李'
}) function getName (name:FullName):string {
return `${name.firstName} -- ${name.secondName}`;
}
getName({firstName:'张', secondName: '三'});

接口,可选属性

interface FullName {
firstName:string;
secondName?:string; // ? 代表可选
}
printName({firstName:'张'});

函数类型接口

对方法传入的参数以及返回值进行约束,批量约束。

interface Encrytp {
(key:string, val:string):string;
} let md5:Encrytp = function (key:string, val:string):string {
// 具体加密省略
return key + val;
}
console.log(md5('name', 'Jane')); // nameJane let hash:Encrytp = function (key:string, val:string):string {
// 具体加密省略
return key + ' ---- ' + val;
}
console.log(hash('name', 'Jane')); // name ---- Jane

可索引接口

对数组、对象的约束(不常用)

ts 中数组的两种定义方式:

let arr1:Array<number> = [1, 2];
let arr2:string[] = ['12', '23'];

ts 中可索引接口,对数组的约束

interface userArr {
[index:number]:string;
}
let arr3:userArr = ['a', 'b'];
console.log(arr3[0]);
// let arr4:userArr = [12, 34]; // 错误

ts 中可索引接口,对对象的约束

interface userObj {
[index:string]:string;
}
let obj1:userObj = {name: 'Jane'};
// let obj2:userObj = {age: 12}; // 错误

类类型接口

对类的约束,和抽象类有点相似。类类型接口实现用关键字implements

interface Animal {
name:string;
eat (food:string):void;
} class Dog implements Animal { // 类类型接口实现用关键字 implements
name:string; constructor (name:string) {
this.name = name;
} eat (food:string):void {
console.log(`${this.name}喜欢吃${food}`);
}
}
let d = new Dog('小黑');
d.eat('骨头'); // 小黑喜欢吃骨头 class Cat implements Animal {
name:string; constructor (name:string) {
this.name = name;
} eat (food:string):void {
console.log(`${this.name}喜欢吃${food}`);
}
}
let c = new Cat('小白');
c.eat('老鼠'); // 小白喜欢吃老鼠

接口扩展

接口可以继承接口

interface Animal {
eat ():void;
}
interface Person extends Animal {
work ():void;
}
class Web implements Person {
name:string;
constructor (name:string) {
this.name = name;
}
// 接口 Animal 中的方法必须实现
eat ():void {
console.log(`${this.name}喜欢吃甜点`);
}
// 接口 Person 中的方法必须实现
work ():void {
console.log(`${this.name}在运动`);
}
}
let w = new Web('小李');
w.eat();
w.work();

继承类并实现接口

interface Animal {
eat ():void;
}
interface Person extends Animal {
work ():void;
} class Programmer {
name:string;
constructor (name:string) {
this.name = name;
} coding () {
console.log(`${this.name}在写代码`);
}
} class Web extends Programmer implements Person { constructor (name:string) {
super(name);
}
// 接口 Animal 中的方法必须实现
eat ():void {
console.log(`${this.name}喜欢吃甜点`);
}
// 接口 Person 中的方法必须实现
work ():void {
console.log(`${this.name}在运动`);
}
}
let w = new Web('小李');
w.eat();
w.work();
w.coding(); // 小李在写代码

Typescript 学习笔记六:接口的更多相关文章

  1. TypeScript学习笔记之接口类型

    TypeScript的接口,个人理解就是一种约束,包括各种类型的契约或者代码定义上的契约.当然,和java中的用法基本一致,接口可以被继承也可以被实现. 定义一个简单的interface interf ...

  2. Typescript 学习笔记七:泛型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  3. Typescript 学习笔记五:类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  4. Typescript 学习笔记四:回忆ES5 中的类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  5. Typescript 学习笔记二:数据类型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  6. Typescript 学习笔记三:函数

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  7. Typescript 学习笔记一:介绍、安装、编译

    前言 整理了一下 Typescript 的学习笔记,方便后期遗忘某个知识点的时候,快速回忆. 为了避免凌乱,用 gitbook 结合 marketdown 整理的. github地址是:ts-gitb ...

  8. java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

    java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...

  9. # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)

    目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...

随机推荐

  1. Controller层aop

    利用@Around通知修改Controller的返回值 自定义一个注解@OperationBtn 在切入点Controller上加上自定义注解 接下来就是重点了,AspectJ写切面类,对该Contr ...

  2. jQuery之必会增删改查Dom操作

    .next  .prev <button>change</button> <span class = '.demo'>aaa</span> <p ...

  3. 用element-ui 时,报value.getTime is not a function错误:

    在用element-ui 时,报value.getTime is not a function错误:错误分析:date-picker 的时间是格林威时间,如果Thu Jun 22 2017 19:07 ...

  4. Install_WordPress_In_CentOS_7

    1 – Install Apache Http Server# yum install httpd.x86_64 2 – Install php# yum install php.x86_64 3 – ...

  5. 最小的K个数(python)

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,.   # -*- coding:utf-8 -*- class So ...

  6. 153. Find Minimum in Rotated Sorted Array找其中的最小值

    [抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: 总算自己写出一道题来了. [英文数据结构 ...

  7. 4412 uboot启动分析

    感谢sea1105, https://blog.csdn.net/sea1105/article/details/52142772 在学习过程中,由于tiny4412资料太过于少,因此参考210的视屏 ...

  8. Entity Framework Core: A second operation started on this context before a previous operation completed

    我这边报错是因为函数声明的是async  void 而实现中有多个task任务,导致的线程不安全

  9. java ssh执行shell脚本

    1.添加依赖 com.jcraft:jsch ch.ethz.ganymed:ganymed-ssh2:262 2.获取连接 conn = new Connection(ip, port); conn ...

  10. tian_lie

    后台托管:nohup ./re_start_job.sh kg_fk_etl >>log.log 2>&1 & 查看进程:ps -ef|grep kg_fk_etl ...