typescript接口的概念 以及属性类型接口
- /*
- 1、vscode配置自动编译
- 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": "./js",
- 2、第二步 任务 - 运行任务 监视tsconfig.json
- 2、typeScript中的数据类型
- typescript中为了使编写的代码更规范,更有利于维护,增加了类型校验,在typescript中主要给我们提供了以下数据类型
- 布尔类型(boolean)
- 数字类型(number)
- 字符串类型(string)
- 数组类型(array)
- 元组类型(tuple)
- 枚举类型(enum)
- 任意类型(any)
- null 和 undefined
- void类型
- never类型
- 3、typeScript中的函数
- 3.1、函数的定义
- 3.2、可选参数
- 3.3、默认参数
- 3.4、剩余参数
- 3.5、函数重载
- 3.6、箭头函数 es6
- 4、typeScript中的类
- 4.1 类的定义
- 4.2 继承
- 4.3 类里面的修饰符
- 4.4 静态属性 静态方法
- 4.5 抽象类 多态
- 5、typeScript中的接口
- 5.1 属性类接口
- 5.2 函数类型接口
- 5.3 可索引接口
- 5.4 类类型接口
- 5.5 接口扩展
- */
- /*
- 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用。接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据,也不关心这些类里方法的实现细节,它只规定这批类里必须提供某些方法,提供这些方法的类就可以满足实际需要。 typescrip中的接口类似于java,同时还增加了更灵活的接口类型,包括属性、函数、可索引和类等。
- 定义标准。
- */
- // 1、属性接口 对json的约束
- //ts中定义方法
- /*
- function printLabel():void {
- console.log('printLabel');
- }
- printLabel();
- */
- /*
- ts中定义方法传入参数
- function printLabel(label:string):void {
- console.log('printLabel');
- }
- printLabel('hahah');
- */
- /*
- ts中自定义方法传入参数,对json进行约束
- */
- /*
- function printLabel(labelInfo:{label:string}):void {
- console.log('printLabel');
- }
- printLabel('hahah'); //错误写法
- printLabel({name:'张三'}); //错误的写法
- printLabel({label:'张三'}); //正确的写法
- */
- //对批量方法传入参数进行约束。
- //接口:行为和动作的规范,对批量方法进行约束
- //就是传入对象的约束 属性接口
- // interface FullName{
- // firstName:string; //注意;结束
- // secondName:string;
- // }
- // function printName(name:FullName){
- // // 必须传入对象 firstName secondName
- // console.log(name.firstName+'--'+name.secondName);
- // }
- // // printName('1213'); //错误
- // var obj={ /*传入的参数必须包含 firstName secondName*/
- // age:20,
- // firstName:'张',
- // secondName:'三'
- // };
- // printName(obj)
- // 接口:行为和动作的规范,对批量方法进行约束
- // interface FullName{
- // firstName:string; //注意;结束
- // secondName:string;
- // }
- // function printName(name:FullName){
- // // 必须传入对象 firstName secondName
- // console.log(name.firstName+'--'+name.secondName);
- // }
- // function printInfo(info:FullName){
- // // 必须传入对象 firstName secondName
- // console.log(info.firstName+info.secondName);
- // }
- // var obj={ /*传入的参数必须包含 firstName secondName*/
- // age:20,
- // firstName:'张',
- // secondName:'三'
- // };
- // printName(obj);
- // printInfo({
- // firstName:'李',
- // secondName:'四'
- // })
- //接口 :可选属性
- // interface FullName{
- // firstName:string;
- // secondName:string;
- // }
- // function getName(name:FullName){
- // console.log(name)
- // }
- // //参数的顺序可以不一样
- // getName({
- // secondName:'secondName',
- // firstName:'firstName'
- // })
- // interface FullName{
- // firstName:string;
- // secondName?:string;
- // }
- // function getName(name:FullName){
- // console.log(name)
- // }
- // getName({
- // firstName:'firstName'
- // })
- /*
- $.ajax({
- type: "GET",
- url: "test.json",
- data: {username:$("#username").val(), content:$("#content").val()},
- dataType: "json"
- });
- */
- interface Config{
- type:string;
- url:string;
- data?:string;
- dataType:string;
- }
- //原生js封装的ajax
- function ajax(config:Config){
- var xhr=new XMLHttpRequest();
- xhr.open(config.type,config.url,true);
- xhr.send(config.data);
- xhr.onreadystatechange=function(){
- if(xhr.readyState==4 && xhr.status==200){
- console.log('chengong');
- if(config.dataType=='json'){
- console.log(JSON.parse(xhr.responseText));
- }else{
- console.log(xhr.responseText)
- }
- }
- }
- }
- ajax({
- type:'get',
- data:'name=zhangsan',
- url:'http://a.itying.com/api/productlist', //api
- dataType:'json'
- })
typescript接口的概念 以及属性类型接口的更多相关文章
- typescript属性类型接口
/* typeScript中的接口 - 1.属性类接口 */ /* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定 ...
- java中接口的概念及使用(补充final修饰符的使用)
接口 初期理解,可以是一个特殊的抽象类 当抽象类中的方法都是抽象的,那么该类可以通过接口的形式来表示 class 用于定义类 interface 用于定义接口 接口定义时,格式特点: 1.接口中常见的 ...
- 机器学习实战基础(三十七):随机森林 (四)之 RandomForestRegressor 重要参数,属性与接口
RandomForestRegressor class sklearn.ensemble.RandomForestRegressor (n_estimators=’warn’, criterion=’ ...
- Typescript中的可索引接口 类类型接口
/* 5.typeScript中的接口 可索引接口 类类型接口 */ /* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用 ...
- TypeScript 高级类型 接口(interface)
在代码的实现或者调用上能设定一定的限制和规范,就像契约一样.通常,我们把这种契约称为接口. TypeScript的核心原则之一是对值所具有的结构进行类型检查. 有时称为“鸭式辨型法”或“结构性子类型化 ...
- typescript可索引接口 类类型接口
/* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据, ...
- typescript函数类型接口
/* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据, ...
- 转载:《TypeScript 中文入门教程》 3、接口
版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 介绍 TypeScript的核心原则之一是对值所具有的shape进行类型检查. 它有时 ...
- TypeScript学习笔记(五):接口
使用接口 在前面的笔记中我们知道可以使用Object Type来指定参数的属性,如下: function printLabel(labelledObj: {label: string}) { cons ...
随机推荐
- linux令普通用户拥有root权限
如题,平时我们在自己电脑上玩linux,基本都是一个root用户就够了(反正我99%时间都是直接用root用户登录系统),可如果在公司里就不行了,有时候需要多个用户对系统具有root权限,类似与一个系 ...
- Gravitational Teleport docker-compose简单运行
Gravitational Teleport 可以作为堡垒机进行使用,为了测试方便使用docker-compose 运行一个all in one 的demo 备注: 官方提供的docker-compo ...
- gaia 开源多语言的pipeline 平台
gaia 是一个支持goalng.java.c++.python,nodejs (还在开发中)的pipeline 平台,我们可以方便的进行pipeline构建的 添加,同时也可以做为sdk 在我们的项 ...
- NPOI之Excel——合并单元格、设置样式、输入公式、设置筛选等
首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...
- c# 数据类型可在在最后的带一个字母
folat的后面要带F或者f: double的后面要带D或者d: decimal的后面要带M或者m: long的后面要带L或者l:
- streaming简介
mapreduce和hdfs采用java实现,默认提供java编程接口 streaming框架允许任何程序语言实现的程序在hadoop mapreduce中使用 streaming方便已有的程序向ha ...
- js-自定义对话框
引用插件 <link rel="stylesheet" type="text/css" href="${ctx }/resources/comm ...
- mysql的变量信息详解
mysql的变量详解 执行show variables命令可以查看MySQL服务器的变量 变量名 默认值 说明 对应的配置文件参数 auto_increment_increment 1 自增长类型的初 ...
- [转]happybase1.0 报错:ThriftPy does not support generating module with path in protocol 'f'
happybase1.0 报错:ThriftPy does not support generating module with path in protocol 'f' 2016-10-12 14: ...
- 项目出现 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法
1. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path ①项 ...