IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> typescript接口与继承 初认知 -> 正文阅读

[JavaScript知识库]typescript接口与继承 初认知

接口

在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。

基本使用

//可选属性 可以对可能存在的属性进行预定义
	interface SquareConfig {
	  color?: string; 
	  width?: number;
	}
	let mySquare = createSquare({color: "black"}); //如果不使用可选属性会报错 缺少width

//只读属性 一些对象属性只能在对象刚刚创建的时候修改其值。
	interface Point {
    readonly x: number;
    readonly y: number;
	}
	//ReadonlyArray<T>
	let a: number[] = [1, 2, 3, 4];
	let ro: ReadonlyArray<number> = a;
	ro[0] = 12; // error!
	ro.push(5); // error!
	ro.length = 100; // error!
	a = ro; // error!
	a = ro as number[]; //可以用类型断言重写:
//额外的属性检查
	//索引签名
	interface SquareConfig {
    	color?: string;
    	width?: number;
    	[propName: string]: any;
	}
	//SquareConfig可以有任意数量的属性,并且只要它们不是color和width,那么就无所谓它们的类型是什么。
//可索引的类型
	interface StringArray {
	  [index: number]: string;
	}
	
	let myArray: StringArray;
	myArray = ["Bob", "Fred"];
	
	let myStr: string = myArray[0];



//在函数中使用
	interface SearchFunc {
	  (source: string, subString: string): boolean;
	}
	let mySearch: SearchFunc;
	mySearch = function(source: string, subString: string) {
	  let result = source.search(subString);
	  return result > -1;
	}
	//对于函数类型的类型检查来说,函数的参数名不需要与接口里定义的名字相匹配。
	let mySearch: SearchFunc;
	mySearch = function(src: string, sub: string): boolean {
	  let result = src.search(sub);
	  return result > -1;
	}

//在类中使用
	//TypeScript也能够用接口来明确的强制一个类去符合某种契约。
	interface ClockInterface {
	    currentTime: Date;
	    setTime(d: Date);
	}
	
	class Clock implements ClockInterface {
	    currentTime: Date;
	    setTime(d: Date) {
	        this.currentTime = d;
	    }
	    constructor(h: number, m: number) { }
	}

用const还是readonly?
做为变量使用的话用 const,若做为属性则使用readonly。

接口继承

//接口继承接口
	interface Shape {
	    color: string;
	}
	
	interface PenStroke {
	    penWidth: number;
	}
	
	interface Square extends Shape, PenStroke {
	    sideLength: number;
	}
	
	let square = <Square>{};
	square.color = "blue";
	square.sideLength = 10;
	square.penWidth = 5.0;
//接口继承类
	//当接口继承了一个类类型时,它会继承类的成员但不包括其实现。 
	//就好像接口声明了所有类中存在的成员,但并没有提供具体实现一样。 
	//接口同样会继承到类的private和protected成员。 
	//这意味着当你创建了一个接口继承了一个拥有私有或受保护的成员的类时,这个接口类型只能被这个类或其子类所实现(implement)。
	class Control {
	    private state: any;
	}
	
	interface SelectableControl extends Control {
	    select(): void;
	}
	//Button不具有s
	class Button extends Control implements SelectableControl {
	    select() { }
	}
	
	class TextBox extends Control {
	    select() { }
	}
	
	// 错误:“Image”类型缺少“state”属性。
	class Image implements SelectableControl {
	    select() { }
	}
	
	class Location {
	
	}
  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:06:48  更:2021-08-04 11:09:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/22 9:27:47-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码