TypeScript中的接口和类

编程入门 行业动态 更新时间:2024-10-23 07:35:41
本文介绍了TypeScript中的接口和类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在C#中,接口和类之间存在巨大差异。实际上,类表示引用类型,因此我们实际上可以创建在该类上建模的对象,而接口则是类签名的合同,以确保存在某种行为。特别是我们不能创建接口实例。

In C# there's a quite huge difference between interfaces and classes. Indeed, a class represents a reference-type, so that we can actually create objects modeled on that class, while interfaces are meant to be contracts that a class sign to in order to ensure the existence of a certain behavior. In particular we can't create instances of interfaces.

接口的全部意义在于暴露行为。一个类通过给出所述行为的一个显式实现来实现它。

The whole point with interfaces is to expose behavior. A class implements it by giving one explicit implementation of said behavior.

在这种情况下,虽然接口可能包含属性,但大多数时候我们由于行为问题而关心接口。所以大多数类型的接口只是行为契约。

In that case, although interfaces may contain properties, most of the time we care about interfaces because of behavioral issues. So most of the type, interfaces are just contracts of behavior.

另一方面,在TypeScript上,我看起来让我感到非常不安,事实上我不止一次见过这个,这就是这个问题的原因。

On TypeScript, on the other hand, I've seem something that made me quite uneasy, and in truth I've seen this more than once, which is the reason for this question.

在一个教程中我看到了这个:

In one tutorial I saw this:

export interface User { name: string; // required with minimum 5 chracters address?: { street?: string; // required postcode?: string; } }

但等一下。为什么用户是一个界面?如果我们认为像C#,用户不应该是一个接口。事实上,看着它,似乎我们正在定义数据类型用户,而不是行为契约。

But wait a minute. Why User is an interface? If we think like C#, User shouldn't be an interface. In truth, looking at it, it seems like we are defining the data type User, instead of a contract of behavior.

像在C#中一样思考,自然就是这样:

Thinking like we do in C#, the natural thing would be this:

export class User { public name: string; public address: Address; } export class Address { public street: string; public postcode: string; }

但这样使用接口就像我们使用类一样,只需定义一个数据类型,而不是定义行为契约,似乎在TypeScript中是非常常见的。

But this thing of using interfaces like we do with classes, to just define a data type, rather than defining a contract of behavior, seems very common in TypeScript.

那么TypeScript中的接口是什么?为什么人们在TypeScript中使用接口,就像我们在C#中使用clases一样?如何在TypeScript中正确使用接口:建立行为契约,或者定义属性和对象应该有哪些?

So what interfaces are meant for in TypeScript? Why do people use interfaces in TypeScript like we use clases in C#? How interfaces should be properly used in TypeScript: to establish contracts of behavior, or to define properties and object should have?

推荐答案

考虑在Javascript中,数据通常通过JSON交换为普通对象:

Consider that in Javascript, data is often exchanged as plain objects, often through JSON:

let data = JSON.parse(someString);

假设这个数据是一个数组用户对象,我们会将其传递给一个函数:

Let's say this data is an array of User objects, and we'll pass it to a function:

data.forEach(user => foo(user))

foo 将输入如下:

function foo(user: User) { ... }

但等等,我们什么都没做新用户!我们应该吗?我们是否必须写一个用户和地图所有数据它,即使结果完全相同,带有属性的对象?不,这只是为了满足类型系统而疯狂,但不会改变运行时的任何内容。一个简单的接口描述了特定对象的外观(行为)就足够了。

But wait, at no point did we do new User! Should we? Should we have to write a class User and map all the data to it, even though the result would be exactly the same, an Object with properties? No, that would be madness just for the sake of satisfying the type system, but not change anything about the runtime. A simple interface which describes how the specific object is expected to look like (to "behave") is perfectly sufficient here.

更多推荐

TypeScript中的接口和类

本文发布于:2023-06-12 05:17:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/652040.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   TypeScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!