TypeScript 接口、类、对象

TypeScript 简易教程
一、TypeScript前导
二、TypeScript基础语法
三、TypeScript变量
四、TypeScript Number+String类型
五、TypeScript运算符
六、TypeScript语句
七、TypeScript函数
八、TypeScript 容器类型
九、TypeScript 接口、类、对象
十、TypeScript 命名空间、模块、声明文件
十一、Promise:JavaScript 异步编程的基石
十二、TypeScript:Async Functions

一、Interface 接口

接口是一系列抽象方法的声明,需要由具体的类去实现,然后第三方就可以通过这组抽象方法调用,让具体的类执行具体的方法---这不动态性就来了

1. 接口定义

typescript
复制代码
interface interface_name { }

示例:

typescript
复制代码
interface IPerson { firstName:string, lastName:string, sayHi: ()=>string }

接口使用使用示例:

typescript
复制代码
var customer:IPerson = { firstName:"Tom", lastName:"Hanks", sayHi: ():string =>{return "Hi there"} } var customer:IPerson = { firstName:"Tom", lastName:"Hanks", sayHi: ():string =>{return "Hi there"} }

2. 字段使用联合类型

示例:

typescript
复制代码
interface RunOptions { program:string; commandline:string[]|string|(()=>string); } // commandline 是字符串 var options:RunOptions = {program:"test1",commandline:"Hello"}; console.log(options.commandline) // commandline 是字符串数组 options = {program:"test1",commandline:["Hello","World"]}; console.log(options.commandline[0]); console.log(options.commandline[1]); // commandline 是一个函数表达式 options = {program:"test1",commandline:()=>{return "**Hello World**";}}; var fn:any = options.commandline; console.log(fn());

示例运行结果:

typescript
复制代码
Hello Hello World **Hello World**

3. 字段使用数组

接口中可以将数组的索引值和元素设置为不同类型,索引值可以是数字或字符串。

示例:数组的元素为字符串类型:

typescript
复制代码
interface namelist { [index:number]:string }

变量的元素都是字符串

typescript
复制代码
var list2:namelist = ["Hello", "World","!"]

变量的元素包含非字符串 非法

typescript
复制代码
var list2:namelist = ["Hello", "World", 123]

4. 继承

  • 允许接口继承多个接口。
  • 继承使用关键字 extends。

单接口继承:

typescript
复制代码
Child_interface_name extends super_interface_name

多接口继承:继承的各个接口使用逗号 (,) 分隔。

typescript
复制代码
Child_interface_name extends super_interface1_name, super_interface2_name,…,super_interfaceN_name

单接口继承示例:

typescript
复制代码
interface Person { age:number } interface Musician extends Person { instrument:string }

多接口继承示例:

typescript
复制代码
interface IParent1 { v1:number } interface IParent2 { v2:number } interface Child extends IParent1, IParent2 { }

二、Class 类

定义类的关键字为 class,后面紧跟类名,类可以包含以下几个模块(类的数据成员):

  • 字段 − 字段是类里面声明的变量。
  • 构造函数 − 类实例化时调用。
  • 方法 − 要执行的操作。
  • 访问控制修饰符:public(默认) 、protected、private

1. 类定义

typescript
复制代码
class class_name { }

类的定义示例:

typescript
复制代码
class Car { // 字段 engine:string; // 构造函数 constructor(engine:string) { this.engine = engine } // 方法 disp():void { console.log("发动机为 : "+this.engine) } }

2. 访问控制修饰符

  • public(默认) : 公有,可以在任何地方被访问。
  • protected : 受保护,可以被其自身以及其子类访问。
  • private : 私有,只能被其定义所在的类访问。
typescript
复制代码
class Encapsulate { str1:string = "hello" private str2:string = "world" } var obj = new Encapsulate() console.log(obj.str1) // 可访问 console.log(obj.str2) // 编译错误, str2 是私有的

3. 构造函数

构造函数是一种特殊的函数,用于创建和初始化类的一个新实例。

在类中定义构造函数,它与类名相同,并且没有返回类型。

typescript
复制代码
class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } }

可以使用参数属性简化构造函数,允许直接在类声明中声明属性。

typescript
复制代码
class Person { constructor(public name: string, public age: number) {} } let person = new Person('Bob', 25);

构造函数的重载,可以为同一个类定义多个构造函数。

typescript
复制代码
class Person { constructor(public name: string); constructor(public name: string, public age: number); constructor(name: string, age?: number) { if (age !== undefined) { this.name = name; this.age = age; } else { this.name = name; } } }

4. 类实例化对象

使用 new 关键字来实例化类的对象:

typescript
复制代码
var object_name = new class_name([ arguments ])

实例化对象示例:

typescript
复制代码
var obj = new Car("Engine 1") // 访问属性 obj.field_name // 访问方法 obj.function_name()

5. 类继承

类继承使用关键字 extends,子类除了不能继承父类的私有成员(方法和属性)和构造函数,其他的都可以继承。

TypeScript 一次只能继承一个类,不支持继承多个类,但 TypeScript 支持多重继承(A 继承 B,B 继承 C)。

typescript
复制代码
class child_class_name extends parent_class_name

继承示例:

typescript
复制代码
class Shape { Area:number constructor(a:number) { this.Area = a } } class Circle extends Shape { disp():void { console.log("圆的面积: "+this.Area) } } var obj = new Circle(223); obj.disp()

5.1 方法重写

重写:子类可以对父类的方法重新定义。

super 关键字是对父类的直接引用,该关键字可以引用父类的属性和方法。

示例:

typescript
复制代码
class PrinterClass { doPrint():void { console.log("父类的 doPrint() 方法。") } } class StringPrinter extends PrinterClass { doPrint():void { super.doPrint() // 调用父类的函数 console.log("子类的 doPrint()方法。") } }

5.2 继承中的构造函数

在派生类中,可以使用 super 关键字来调用基类的构造函数。

示例:

typescript
复制代码
class Person { constructor(public name: string); constructor(public name: string, public age: number); constructor(name: string, age?: number) { if (age !== undefined) { this.name = name; this.age = age; } else { this.name = name; } } }

6. 类变量和方法

类变量和方法:static 关键字用于定义类的数据成员(属性和方法)为静态的,静态成员可以直接通过类名调用。

示例:

typescript
复制代码
class StaticMem { static num:number; static disp():void { console.log("num 值为 "+ StaticMem.num) } } StaticMem.num = 12 // 初始化静态变量 StaticMem.disp() // 调用静态方法

7. instanceof

instanceof 运算符用于判断对象是否是指定的类型,如果是返回 true,否则返回 false。

示例:

typescript
复制代码
class Person{ } var obj = new Person() var isPerson = obj instanceof Person;

三、类和接口

类可以实现接口,使用关键字 implements,并将 interest 字段作为类的属性使用。

示例:

typescript
复制代码
interface ILoan { interest:number } class AgriLoan implements ILoan { interest:number rebate:number constructor(interest:number,rebate:number) { this.interest = interest this.rebate = rebate } }

四、对象

对象是包含一组键值对的实例。 值可以是标量、函数、数组、对象等,如下实例:

typescript
复制代码
var object_name = { key1: "value1", // 标量 key2: "value", key3: function() { // 函数 }, key4:["content1", "content2"] //集合 }

示例:

typescript
复制代码
var sites = { site1:"Hello", site2:"World" };

TypeScript不能动态的添加方法。

承接上面示例👆🏻:下面的语句在TypeScript就是非法的。

typescript
复制代码
sites.sayHello = function(){ return "hello";}
分类: 前端扫盲 标签: TypeScriptJavaScriptClass对象

评论

暂无评论数据

暂无评论数据

目录