TypeScript 何时使用接口和类

TypeScript 何时使用接口和类

TypeScript支持类和接口等面向对象的编程特性。类是对象的骨架,封装了对象中使用的数据。接口在TypeScript中就像是类的类型,用于类型检查。它只包含对象成员的声明,有助于派生类。

何时在TypeScript中使用接口:

在创建对象、将对象作为参数传递时,我们应该使用接口来验证对象的结构。接口仅在TypeScript代码中可用,当它编译为JavaScript代码时,它的存在消失了,不会使我们的最终源代码变得更重。当我们想要进行类型检查但又不希望代码变得笨重时,应该在代码中使用接口。

语法:

// Declaring Interface
interface Interface_name {
    ...
} 

示例: 下面的示例将说明如何在TypeScript中使用接口。

JavaScript

interface Example {
    Name_should: string;
    Number_should: number;
}
 
class Class_Example implements Example {
 
    Name_should: string;
    Number_should: number;
    constructor(Name_should: string, 
    Number_should: number) {
        this.Name_should = Name_should;
        this.Number_should = Number_should;
    }
 
}
var k = new Class_Example("Interface", 1);
console.log(k);

输出:

Class_Example { Name_should: 'Interface', Number_should: 1 }

我们已经知道何时使用接口,现在讨论何时在TypeScript中使用类:

何时在TypeScript中使用类:

示例1: 类用作对象工厂。工厂是一种将所有相关数据和函数集合在一个地方的地方,以便我们在工作代码和其他不相关的工作中拥有灵活性而不会造成任何问题。在这种情况下,当我们需要在复杂的代码中灵活性而没有中断时,我们应该使用类。

语法:

// Declaring Classes
class Class_name {
    ...
}

示例: 下面的示例将演示在TypeScript中使用类的方法。

Javascript

var Name = "Geeks1";
var Number1 = 33;
class Class_Example {
 
    Name: string;
    Number: number;
    constructor(Name: string, Number: number) {
        this.Name = Name;
        this.Number = Number1;
    }
 
 
}
var k = new Class_Example("Geeks2", 1);
 
console.log(k.Name);
console.log(Name);
console.log(k.Number);
console.log(Number1);

输出:

Geeks2
Geeks1
1
33

示例2: 类提供了面向对象编程的特性,因此它使我们的代码更易用、可维护和可扩展,因此在TypeScript中应该使用类。类的功能在处理大型代码库时非常有用。因此,当我们需要处理大量代码并且想要使用这些类的功能时,我们应该使用类。例如,我们可以看到类的继承的示例。

示例: 下面的示例将说明在TypeScript中使用类的用法。

Javascript

class vehicle {
 
    Company_Name: string;
    Number: number;
    constructor(Company_Name: string, Number: number) {
        this.Company_Nameclass vehicle {
 
            Company_Name: string;
            Number: number;
            constructor(Company_Name: string, Number: number) {
                this.Company_Name = Company_Name;
                this.Number = Number;
            }
        }
        class car extends vehicle {
            name: string;
            Number: number;
            wheel: number;
            fuel: string;
            constructor(name: string, Number: number,
                wheel: number, fuel: string) {
                super(name, Number);
                this.wheel = wheel;
                this.fuel = fuel;
            }
        }
        var k = new car("toyota", 9243, 4, "Geeks2");
        e = Company_Name;
        this.Number = Number;
    }
}
class car extends vehicle {
    name: string;
    Number: number;
    wheel: number;
    fuel: string;
    constructor(name: string, Number: number,
        wheel: number, fuel: string) {
        super(name, Number);
        this.wheel = wheel;
        this.fuel = fuel;
    }
}
var k = new car("toyota", 9243, 4, "Geeks2");

输出结果:

car { 
    Company_Name: 'toyota', 
    Number: 9243, 
    wheel: 4, 
    fuel: 'Geeks2' 
}

类和接口的优势:

接口
类提供继承,使得一个类可以借用另一个类的属性。 接口也提供了继承属性。
类支持方法重写,有助于函数重新定义。 它与具有相同编号的接口合并。
类提供了一个静态方法,使得类的函数可以在不声明的情况下使用。 它不会增加源文件的大小,因为它是一种虚拟结构。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程