Typescript中的接口和抽象类有什么区别? [英] What is the difference between interface and abstract class in Typescript?

查看:243
本文介绍了Typescript中的接口和抽象类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了几行代码进行实验并区分这两个代码:interfaceabstract class.

I wrote a couple line of code to experiment and differentiate between these two: interface and abstract class.

我发现它们有相同的限制.

I found out that they have the same restriction.

interface IPerson {
  name: string;
  talk(): void;
}

interface IVIP {
  code: number;
}

abstract class Person {
  abstract name: string;
  abstract talk(): void;
}

class ManagerType1 extends Person {
  // The error i get is that i need to implement the talk() method
  // and name property from its base class.
}

class ManagerType2 implements IPerson {
  // The error i get is that i need to implement the talk() method 
  // and the name property from the interface.
}


class ManagerType3 implements IPerson, IVIP {
  // Now the error i get is that i need to implement all the 
  // properties and methods of the implemented interfaces to the derive class
}

正如我发现的那样,这两者之间没有明显的区别,因为它们都实现了相同的限制.我唯一注意到的是 inhereance 实施.

As what i found is, there are no clear differences between these two since they both implements the same restriction. The only thing i notice is inheretance and implementation.

  1. 一个类只能扩展到单个基类
  2. 一个类可以实现多个接口.

我没听错吗?如果是这样,什么时候需要使用一个?

Did i catch it right? If so when do i need to use one?

更新

我不知道这是否是正确的答案,但您可以根据自己的情况选择同时使用. OOP真的很棒.

I Dont know if is the right answer but you can really use BOTH depending to your situation. OOP is really cool.

class ManagerType3 extends Person implements IPerson, IVIP {
  // Now the restriction is you need to implement all the abstract
  // properties and methods in the based class and all 
  // the properties and methods from the interfaces
}

推荐答案

接口

interface合约,用于定义属性以及实现该属性的对象可以执行的操作.例如,您可以定义管道工电工的功能:

Interfaces

An interface is a contract that defines the properties and what the object that implements it can do. For example, you could define what can do a Plumber and an Electrician:

interface Electrician {
  layWires(): void
}

interface Plumber {
  layPipes(): void
}

然后,您可以使用接口的服务:

Then, you can consume the services of your interfaces:

function restoreHouse(e: Electrician, p: Plumber) {
  e.layWires()
  p.layPipes()
}

请注意,实现接口的方法是免费的.您可以通过实例化一个类或使用一个简单的对象来做到这一点:

Notice that the way you have to implement an interface is free. You can do that by instantiating a class, or with a simple object:

let iAmAnElectrician = {
  layWires: () => { console.log("Work with wires…") }
}

在运行时根本不存在任何接口,因此无法进行自省.这是处理对象编程的经典JavaScript方法,但是在定义合同的编译时具有良好的控制能力.

An interface doesn't exist at all at runtime, so it is not possible to make an introspection. It is the classic JavaScript way to deal with object programming, but with a good control at compile time of the defined contracts.

class既是合同又是工厂的实施. abstract class也是一种实现,但不完整.尤其是,即使只有抽象方法(也可以使用instanceof),抽象类仍在运行时存在.

A class is both a contract and the implementation of a factory. An abstract class is also an implementation but incomplete. Especially, an abstract class exists at runtime, even if it has only abstract methods (then instanceof can be used).

定义抽象类时,通常会尝试控制流程的实现方式.例如,您可以编写如下内容:

When you define an abstract class, you often try to control how a process has to be implemented. For example, you could write something like this:

abstract class HouseRestorer {
  protected abstract layWires(): void
  protected abstract layPipes(): void
  restoreHouse() {
    this.layWires()
    this.layPipes()
  }
}

此抽象类HouseRestorer定义了如何使用方法layWireslayPipes,但是在使用特殊方法之前,要由子类来实现专门的处理.

This abstract class HouseRestorer defines how the methods layWires and layPipes will be used, but it is up to a child class to implement the specialized treatments before it can be used.

抽象类是一种传统的OOP方法,不是JavaScript中的传统方法.

Abstract classes are a traditional OOP approach, which is not traditional in JavaScript.

两种方法都允许完成相同的操作.但是它们是解决问题的两种不同方式.那并不意味着他们彼此值得.

Both approaches allow the same things to be done. But they are two different ways of solving a problem. That doesn't mean they're worth each other.

这篇关于Typescript中的接口和抽象类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆