📜  TypeScript 中接口和类的区别

📅  最后修改于: 2022-05-13 01:56:46.664000             🧑  作者: Mango

TypeScript 中接口和类的区别

在本文中,我们将了解 TypeScript 中“接口”和“类”之间的区别。

接口:接口是用于类型检查的虚拟结构。在 TypeScript 中,我们使用 interface 关键字来创建具有标识的新接口。它为相同的数据类型创建结构。接口是定义具有名称和类型的对象的属性和方法的结构。

句法:

interface New_Interface{ // This is interface Body }

特征:

  • 它具有松耦合。
  • 它支持多重继承。

示例 1:

Javascript
// Interface for Class
interface ForClass {
    readonly var1:string;               
}
  
let newclass: ForList = {var1:"Interface"};
console.log(newclass);


Javascript
// Interface for Object with extends function 
interface ForObj {
    First: string
}
  
interface forNewObj extends ForObj {
    Second: number
}
  
let newArray: forNewObj = {
    First: "Interface for Object",
    Second: 2
};
  
console.log(newArray);


Javascript
const Table_Object = {
                      
    // field of class
    Size : 32,
                   
    // Collection field 
    contents : ["Book","Pen"],
                   
    // Function 
    prtint : function() {
        console.log("hello Geeks")
    }
}
   
console.log(Table_Object);


Javascript
class Geeks {      
    name : string ;
    articles: number ; 
    cp_problems: number;
  
    // Constructor of class
    constructor(name:string, articles: number, cp_problems: number) {
        this.name = name;
        this.articles = articles;
        this.cp_problems = cp_problems;
    }
                   
    // About object 
    About() : void {
        console.log("Name of Geeks: " + this.name );
        console.log("No. of articles by Geeks: "
            + this.articles);
        console.log("No. of cp problems sol by Geeks: "
            + this.cp_problems)
    }
}
var geek1 = new Geeks("Abhinav", 87, 560);
geek1.About();


输出:

{ var1: 'Interface' }

示例 2:

Javascript

// Interface for Object with extends function 
interface ForObj {
    First: string
}
  
interface forNewObj extends ForObj {
    Second: number
}
  
let newArray: forNewObj = {
    First: "Interface for Object",
    Second: 2
};
  
console.log(newArray);

输出:

{ First: 'Interface for Object', Second: 2 }

类:它们是对象的骨架,我们使用它来实现对象。在 TypeScript 中,我们使用 class Keyword 来为对象创建构造函数。它可以有属性、方法和变量。

句法:

class geeks {
    // Class property and methods
    // are created here
}

特征:

  • 在类中,它支持成员可见性。
  • 它支持成员覆盖。
  • 它支持继承。

示例 1:

Javascript

const Table_Object = {
                      
    // field of class
    Size : 32,
                   
    // Collection field 
    contents : ["Book","Pen"],
                   
    // Function 
    prtint : function() {
        console.log("hello Geeks")
    }
}
   
console.log(Table_Object);

输出:

{ 
    Size: 32, 
    contents: [ 'Book', 'Pen' ], 
    prtint: [Function: prtint] 
}

示例 2:

Javascript

class Geeks {      
    name : string ;
    articles: number ; 
    cp_problems: number;
  
    // Constructor of class
    constructor(name:string, articles: number, cp_problems: number) {
        this.name = name;
        this.articles = articles;
        this.cp_problems = cp_problems;
    }
                   
    // About object 
    About() : void {
        console.log("Name of Geeks: " + this.name );
        console.log("No. of articles by Geeks: "
            + this.articles);
        console.log("No. of cp problems sol by Geeks: "
            + this.cp_problems)
    }
}
var geek1 = new Geeks("Abhinav", 87, 560);
geek1.About();

输出:

Name of Geeks: Abhinav
No. of articles by Geeks: 87
No. of cp problems sol by Geeks: 560

接口和类的区别如下:

Interface

Classes

We can Create the interface with the use of the interface keyword.

i.e interface Interface_Name{   \\ Interface Body }

We can create the class with class keyword.

i.e class Class_Name{ \\ Class Body }

The interfaceblueprint is mainly the Type structure of object. i.e It is object with only defining the type of parameter inside.Class is the blueprint of the object i.e.the create purposes class is how we implement the object of our code. 
It is used for type checking purpose. Use of interface if TypeScript language is mainly focused on the checking the type of parameters in object.Classes in Types script is used to made the object for something. It is used for implementing the object.
We cannot create the instance of interface with new in typescript. It means that we cannot create the copy of instance in Typescript.We can create a new instance of the class in TypeScript. It means that we can create the copy of class with new keyword.
Interface is virtual structure. Means it only present in TypeScript code not in TypeScript compiled JavaScript code. It always exists in code after the compilation of TypeScript to JavaScript.