typescript中的class和interface

开发
刚刚的vue3.0一发布,各大网址和社区以及公众号已经被Vue3.0的One Piece版本所霸屏,出现不同的标题有着同样内容的现象,借此热度我们不如好好回顾一下ts基础知识,备战vue3.0的正式使用。

[[343370]]

 typescript这个东西说实在的,真的是容易忘记,一段时间不用就感觉特别陌生,但是回过头来看看,又有一种熟悉的感觉,有句话这么说的ts越用越香,它确实能够规范我们的书写的格式,语法校验和类型校验等。之前写过react+ts的一个demo,但是时间久了就忘记了,现在也是趁着热度再回顾一下ts的内容,以及一些高阶语法,现在我们回顾一下ts中常见的类和接口,如果喜欢的可以点赞,评论,关注公众号让更多的人看到。如果有问题也可以评论留言,我们一起相互学习,一起进步。

class
首页我们要清楚的一点是typescript中类和javascript中ES6语法类的区别,千万不要混淆。ts中相比于js添加了声明属性的类型和参数的类型以及返回结果类型。这个地方一看就会一写就不对,如果不声明ts会报错。

  1. class Person{ 
  2.     name:string; 
  3.     constructor(name:string){ 
  4.         this.name = name
  5.     } 
  6.     getName():void{ 
  7.         console.log(this.name); 
  8.     } 

  1. class Person{ 
  2.     constructor(name){ 
  3.         this.name = name
  4.     } 
  5.     getName(){ 
  6.         console.log(this.name); 
  7.     } 

ES5编辑后的结果

  1. var Person = /** @class */ (function () { 
  2.     function Person(name) { 
  3.         this.name = name
  4.     } 
  5.     Person.prototype.getName = function () { 
  6.         console.log(this.name); 
  7.     }; 
  8.     return Person; 
  9. }()); 

类的get和set
ts在编译get和set的时候默认是es3编译,vscode编辑器会报错error TS1056: Accessors are only available when targeting ECMAScript 5 and higher需要编译到版本ES5或以上,解决办法:$ tsc xxx.ts -t es5。

  1. class User
  2.     myname:string; 
  3.     constructor(myname:string){ 
  4.         this.myname = myname 
  5.     } 
  6.     get name(){ 
  7.         return this.myname 
  8.     } 
  9.     set name(newName:string){ 
  10.         this.myname = newName 
  11.     } 

ES5编译后的结果

  1. var User = /** @class */ (function () { 
  2.     function User(myname) { 
  3.         this.myname = myname; 
  4.     } 
  5.     Object.defineProperty(User.prototype, "name", { 
  6.         get: function () { 
  7.             return this.myname; 
  8.         }, 
  9.         setfunction (newName) { 
  10.             this.myname = newName; 
  11.         }, 
  12.         enumerable: false
  13.         configurable: true 
  14.     }); 
  15.     return User
  16. }()); 

这里有几个思考问题,答案见文末:

  1. var u = new User("a"); 
  2. console.log(u); 
  3. console.log(u.myname); 
  4. u.myname = 'b'
  5. console.log(u.myname); 
  6. console.log(u.hasOwnProperty("name")); 
  7. console.log(Object.getPrototypeOf(u)); 
  8. console.log(Object.getPrototypeOf(u).hasOwnProperty("name")); 

抽象类
abstract关键字表示抽象类,抽象类是不能被实例化即new,只能被继承,抽象类一般是用来封装一些公共的,提供给子类使用的方法和属性的

  1. abstract class Animal{ 
  2.     public readonly name:string; 
  3.     protected age:number = 38; 
  4.     private money:number = 10; 
  5.     constructor(name:string){ 
  6.         this.name = name 
  7.     } 
  8. class Dog extends Animal{ 
  9.     static className = 'Dog' 
  10.     static getClassName(){ 
  11.         console.log(this.className) 
  12.     } 
  13.     getName(){ 
  14.         console.log(this.name
  15.     } 
  16.     getAge(){ 
  17.         console.log(this.age) 
  18.     } 
  19. let a = new Animal("ss"); 

这里打印看一下继承的结果:

  1. console.log(a); //Dog { age: 38, money: 10, name'ss' } 

这里顺便说一下访问修饰符 public protected private

public 里里外外都能访问,包括自己、自己的子类、其他类都能
protected 自己和子类能访问但是其他地方不能访问
private 私有的(只有自己能访问,子类的其他都不能访问)

接口表示对象的属性

  1. interface Rectangle { 
  2.     width: number; 
  3.     height: number 
  4.  
  5. let r: Rectangle = { 
  6.     width: 100, height: 10 
  7.  
  8. interface Speakable { 
  9.     speak(): void; 
  10.     name?: string 
  11.  
  12. let speaker: Speakable = { 
  13.     //name:"bdt"
  14.     speak() { } 

接口用来描述抽象的行为

  1. interface AnimalLink { 
  2.     eat(): void; 
  3.     move(): void 

接口可以实现继承

  1. interface PersonLike extends AnimalLink { 
  2.     speak(): void 
  3. class Person2 implements PersonLike { 
  4.     speak() { }; 
  5.     eat() { }; 
  6.     move() { } 

通过接口约束变量类型

  1. interface Person3 { 
  2.     readonly id: number; 
  3.     name: string; 
  4.     [PropName: string]: any 
  5. let p1: Person3 = { 
  6.     id: 1, 
  7.     name"sss" 

通过接口约束(规范)函数

  1. interface DiscountInterface{ 
  2.     (price:number):number 
  3. let discount:DiscountInterface = function (price: number): number { 
  4.     return price * .8 

通过索引约束数组和对象

  1. interface UserInterface{ 
  2.     [index:number]:string 
  3.  
  4. let arr:UserInterface = ['aa','bb'
  5.  
  6. interface UserInterface2{ 
  7.     [index:string]:string 
  8. let obj:UserInterface2  = {name:"sss"

通过接口约束构造函数

  1. class Animal3{ 
  2.     constructor(public name:string){} 
  3. interface WithClassName{ 
  4.     new (name:string):Animal3 
  5. function createClass(clazz:WithClassName,name:string){ 
  6.     return new clazz(name
  7. let a3 = createClass(Animal3,"别抖腿"); 
  8. console.log(a3) 

class和interface的区别
class 类声明并实现方法
interface 接口声明,但是不能实现方法

  1. abstract class Animal{ 
  2.     name:string="111"
  3.     abstract speak():void;  //抽象类和方法不包含具体实现  必须在子类中实现 
  4. //接口里的方法都是抽象的 
  5. interface Flying{ 
  6.     fly():void 
  7. interface Eating{ 
  8.     eat():void 
  9. class Dog extends Animal{ 
  10.     speak(){ 
  11.         console.log("汪汪汪")   //重写:子类重写继承自父类中的方法 
  12.     } 
  13. class Cat extends Animal implements Flying,Eating{ 
  14.     speak(){   //继承抽象类的方法必须实现 
  15.         console.log("喵喵喵"
  16.     } 
  17.     fly(){ 
  18.         console.log("我是一只飞货"
  19.     } 
  20.     eat(){ 
  21.         console.log("我是一只吃货"
  22.     } 

写在最后
文中答案

  1. User { myname: 'a' } 
  2. false 
  3. User { name: [Getter/Setter] } 
  4. true 

 

 

责任编辑:姜华 来源: 小丑的小屋
相关推荐

2022-09-02 09:02:44

TypeInterface

2009-08-27 15:48:40

interfaceabstract cl

2009-08-27 16:22:58

interfaceabstract cl

2022-05-06 09:21:21

TypeScriptinterfacetype

2021-08-05 08:32:45

TypeScript InterfaceType

2021-06-23 08:01:18

TypeScript interface type

2022-11-30 07:17:53

2021-08-04 08:33:59

TypeScriptConst Readonly

2021-07-27 06:06:34

TypeScript语言运算符

2011-07-06 15:14:34

iOS Xcode

2023-12-07 11:47:00

TypeScript特殊值

2023-03-30 07:52:03

Golang接口

2022-04-11 08:42:09

TypeScript子类型定义

2022-08-08 09:00:42

TypeScript映射类型

2012-12-24 13:30:34

iOS

2021-08-02 07:57:03

SynchronizeJava语言

2010-09-14 15:24:49

CSSIDClass

2021-08-24 10:25:19

thisclassJava

2023-04-14 15:44:20

TypeScrip函数重载

2023-11-28 14:04:57

TypeScript遍历对象键
点赞
收藏

51CTO技术栈公众号