广告
返回顶部
首页 > 资讯 > 精选 >TypeScript中的class和interface怎么用
  • 222
分享到

TypeScript中的class和interface怎么用

2023-06-27 10:06:27 222人浏览 安东尼
摘要

这篇文章主要介绍了typescript中的class和interface怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇TypeScript中的class和interface怎么用文章都会有所收获,下面我们

这篇文章主要介绍了typescript中的class和interface怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇TypeScript中的class和interface怎么用文章都会有所收获,下面我们一起来看看吧。

class

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

class Person{    name:string;    constructor(name:string){        this.name = name;    }    getName():void{        console.log(this.name);    }}
class Person{    constructor(name){        this.name = name;    }    getName(){        console.log(this.name);    }}

ES5编辑后的结果

var Person =  (function () {    function Person(name) {        this.name = name;    }    Person.prototype.getName = function () {        console.log(this.name);    };    return Person;}());
类的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

class User{    myname:string;    constructor(myname:string){        this.myname = myname    }    get name(){        return this.myname    }    set name(newName:string){        this.myname = newName    }}

ES5编译后的结果

var User =  (function () {    function User(myname) {        this.myname = myname;    }    Object.defineProperty(User.prototype, "name", {        get: function () {            return this.myname;        },        set: function (newName) {            this.myname = newName;        },        enumerable: false,        configurable: true    });    return User;}());

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

var u = new User("a");console.log(u);console.log(u.myname);u.myname = 'b';console.log(u.myname);console.log(u.hasOwnProperty("name"));console.log(Object.getPrototypeOf(u));console.log(Object.getPrototypeOf(u).hasOwnProperty("name"));
抽象类

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

abstract class Animal{    public readonly name:string;    protected age:number = 38;    private money:number = 10;    constructor(name:string){        this.name = name    }}class Dog extends Animal{    static className = 'Dog'    static getClassName(){        console.log(this.className)    }    getName(){        console.log(this.name)    }    getAge(){        console.log(this.age)    }}let a = new Animal("ss");

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

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

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

  • public 里里外外都能访问,包括自己、自己的子类、其他类都能

  • protected 自己和子类能访问但是其他地方不能访问

  • private 私有的(只有自己能访问,子类的其他都不能访问)

interface

接口表示对象的属性
interface Rectangle {    width: number;    height: number}let r: Rectangle = {    width: 100, height: 10}interface Speakable {    speak(): void;    name?: string}let speaker: Speakable = {    //name:"bdt",    speak() { }}
接口用来描述抽象的行为
interface AnimalLink {    eat(): void;    move(): void}
接口可以实现继承
interface PersonLike extends AnimalLink {    speak(): void}class Person2 implements PersonLike {    speak() { };    eat() { };    move() { }}
通过接口约束变量类型
interface Person3 {    readonly id: number;    name: string;    [PropName: string]: any}let p1: Person3 = {    id: 1,    name: "sss"}
通过接口约束(规范)函数
interface DiscountInterface{    (price:number):number}let discount:DiscountInterface = function (price: number): number {    return price * .8}
通过索引约束数组和对象
interface UserInterface{    [index:number]:string}let arr:UserInterface = ['aa','bb']interface UserInterface2{    [index:string]:string}let obj:UserInterface2  = {name:"sss"}
通过接口约束构造函数
class Animal3{    constructor(public name:string){}}interface WithClassName{    new (name:string):Animal3}function createClass(clazz:WithClassName,name:string){    return new clazz(name)}let a3 = createClass(Animal3,"别抖腿");console.log(a3)

class和interface的区别

  • class 类声明并实现方法

  • interface 接口声明,但是不能实现方法

abstract class Animal{    name:string="111";    abstract speak():void;  //抽象类和方法不包含具体实现  必须在子类中实现}//接口里的方法都是抽象的interface Flying{    fly():void}interface Eating{    eat():void}class Dog extends Animal{    speak(){        console.log("汪汪汪")   //重写:子类重写继承自父类中的方法    }}class Cat extends Animal implements Flying,Eating{    speak(){   //继承抽象类的方法必须实现        console.log("喵喵喵")    }    fly(){        console.log("我是一只飞货")    }    eat(){        console.log("我是一只吃货")    }}

文中答案

User { myname: 'a' }ab falseUser { name: [Getter/Setter] }true

关于“TypeScript中的class和interface怎么用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“TypeScript中的class和interface怎么用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。

--结束END--

本文标题: TypeScript中的class和interface怎么用

本文链接: https://www.lsjlt.com/news/309736.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • TypeScript中的class和interface怎么用
    这篇文章主要介绍了TypeScript中的class和interface怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇TypeScript中的class和interface怎么用文章都会有所收获,下面我们...
    99+
    2023-06-27
  • TypeScript的class类怎么用
    本篇内容介绍了“TypeScript的class类怎么用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!class 类类是面向对象语言的程序设...
    99+
    2023-06-29
  • typeScript的interface接口怎么定义使用
    这篇“typeScript的interface接口怎么定义使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“typeScri...
    99+
    2023-06-29
  • TypeScript中interface和type间的区别是什么
    这篇文章主要介绍“TypeScript中interface和type间的区别是什么”,在日常操作中,相信很多人在TypeScript中interface和type间的区别是什么问题上存在疑惑,小编查阅了各式...
    99+
    2022-10-19
  • abstract class和interface的区别是什么
    本文小编为大家详细介绍“abstract class和interface的区别是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“abstract class和interface的区别是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢...
    99+
    2023-06-03
  • abstract class和interface的本质是什么
    本篇内容介绍了“abstract class和interface的本质是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!我们知道abstr...
    99+
    2023-06-17
  • typescript中type和interface的区别有哪些
    目录前言type和interface的相同点type和interface的不同点结语如何选择 Interface 、 Type总结前言 在typescript里面,有两个概念十分容易...
    99+
    2022-11-13
  • TypeScript中interface和type间的区别有哪些
    这篇文章主要讲解了“TypeScript中interface和type间的区别有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“TypeScript中interface和type间的区别有...
    99+
    2023-06-05
  • TypeScript中正确使用interface和type的方法实例
    目录前言interface type 附:interface和type不同点总结前言 interface 和 type 都是用来定义类型,可以理解定义 shape ,那么 shape...
    99+
    2022-11-12
  • JavaScript和TypeScript中class的示例分析
    这篇文章主要介绍了JavaScript和TypeScript中class的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1、静态成员...
    99+
    2022-10-19
  • TypeScript中type和interface的区别及注意事项
    目录前言概念typeinterface异同点不同点相同点补充:Ts中type和interface定义类型扩展类型的方法总结前言 在 TS 中,type 和 interface相似,都...
    99+
    2022-11-13
    ts type interface ts type interface区别 ts type关键字
  • 怎么使用Java中的abstract和interface
    本篇内容介绍了“怎么使用Java中的abstract和interface”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1、简介abstrac...
    99+
    2023-06-25
  • Java8中stream和functional interface怎么用
    这篇文章主要为大家展示了“Java8中stream和functional interface怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java8中stream和functional i...
    99+
    2023-05-30
    java8 stream functional
  • Typescript中interface与type的相同点与不同点是什么
    今天小编给大家分享一下Typescript中interface与type的相同点与不同点是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一...
    99+
    2023-07-04
  • Golang中的interface怎么使用
    这篇文章主要讲解了“Golang中的interface怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang中的interface怎么使用”吧!万能类型interface在Jav...
    99+
    2023-06-27
  • Typescript中null和undefined怎么用
    这篇文章主要介绍Typescript中null和undefined怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!null和undefined在javascript中null表示...
    99+
    2022-10-19
  • php interface怎么定义和使用
    在PHP中,可以使用interface关键字来定义一个接口。接口是一种规范,用于定义类应该实现的方法。一个类可以实现一个或多个接口,...
    99+
    2023-10-22
    php
  • TypeScript中怎么使用getter和setter
    这篇文章主要介绍“TypeScript中怎么使用getter和setter”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“TypeScript中怎么使用getter和setter”文章能帮助大家解决问...
    99+
    2023-07-05
  • php接口中interface怎么用
    这篇文章给大家分享的是有关php接口中interface怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。PHP开发环境搭建工具有哪些一、phpStudy,是一个新手入门最常用的开发环境。二、WampServe...
    99+
    2023-06-14
  • C#中interface接口怎么用
    小编给大家分享一下C#中interface接口怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!对C# interface接口接触没多长时间,对此起初感觉很简单...
    99+
    2023-06-17
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作