TypeScript中的“扩展"和“实现"之间有什么区别 [英] What's the difference between 'extends' and 'implements' in TypeScript

查看:173
本文介绍了TypeScript中的“扩展"和“实现"之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 man child 的共同点以及它们之间的区别.

I would like to know what man and child have in common and how they differ.

class Person {
  name: string;
  age: number;
}
class child extends Person {}
class man implements Person {}

推荐答案

简短版

  • extends 的意思是:
  • 新班是孩子.它从继承中获得收益.它具有所有属性,方法作为其父级.它可以覆盖其中一些并实现新功能,但已经包含了父级内容.

    The new class is a child. It gets benefits coming with inheritance. It has all properties, methods as its parent. It can override some of these and implement new, but the parent stuff is already included.

    • implements 的意思是:

    新课程可以被视为相同的形状" ,而它不是孩子 >.可以将其传递给需要Person的任何方法,而不管其父级是否不同于Person

    The new class can be treated as the same "shape", while it is not a child. It could be passed to any method where the Person is required, regardless of having different parent than Person

    OOP (如C#,Java语言)我们将使用

    extends 从继承中获利(请参阅 Wiki ).小引用:

    ...在大多数基于类的面向对象的语言中,继承是一种机制,其中一个对象获取父对象的所有属性和行为.继承使程序员可以:创建在现有类之上构建的类...

    ... Inheritance in most class-based object-oriented languages is a mechanism in which one object acquires all the properties and behaviours of the parent object. Inheritance allows programmers to: create classes that are built upon existing classes ...

    implements 将更多地用于多态(请参见 wiki ).小引用:

    ...多态是为不同类型的实体提供单个接口...

    ... polymorphism is the provision of a single interface to entities of different types...

    因此,我们可以拥有与class Man完全不同的继承树.

    So, we can have really different inheritance tree of our class Man.

    class Man extends Human ...
    

    但是如果我们也声明可以假装为其他类型-Person:

    but if we also declare that we can pretend to be a different type - Person:

    class Man extends Human 
              implements Person ...
    

    ..然后我们可以在需要Person的任何地方使用它.我们只需要实现Persons的"interface" (即实施其所有公共物品).

    .. then we can use it anywhere, where the Person is required. We just have to fulfill Persons's "interface" (i.e. implement all its public stuff).

    JavaScript的漂亮面孔(好处之一)是Duck键入的内置支持(请参阅Wiki ).小引用:

    Javascript's nice face (one of the benefits) is the built-in support of the Duck typing (see wiki). Small cite:

    如果它走路像鸭子,却像鸭子一样嘎嘎叫,那它一定是鸭子."

    "If it walks like a duck and it quacks like a duck, then it must be a duck."

    因此,在Javascript中,如果两个不同的对象...将具有一个相似的方法(例如render()),则可以将它们传递给期望它的函数:

    So, in Javascript, if two different objects... would have one similar method (e.g. render()) they can be passed to a function which expects it:

    function(engine){
      engine.render() // any type implementing render() can be passed
    }
    

    为了不失这一点-我们可以在Typescript中做同样的事情-带有更多类型化的支持.那就是那里

    To not lose that - we can in Typescript do the same - with more typed support. And that is where

    class implements class
    

    发挥作用,在有意义的地方

    has its role, where it makes sense

    在OOP语言中,如C# ...没办法...

    In OOP languages as C# ... no way to do that...

    扩展类的接口

    当接口类型扩展类类型时,它会继承以下类型的成员 类,但不是它们的实现.好像接口有 声明了班级的所有成员,但没有提供 执行.接口甚至继承私有和受保护的接口 基类的成员.这意味着当您创建界面时 扩展带有私有或受保护成员的类,该接口 类型只能由该类或其子类实现.

    Interfaces Extending Classes

    When an interface type extends a class type it inherits the members of the class but not their implementations. It is as if the interface had declared all of the members of the class without providing an implementation. Interfaces inherit even the private and protected members of a base class. This means that when you create an interface that extends a class with private or protected members, that interface type can only be implemented by that class or a subclass of it.

    当您具有较大的继承层次结构但又需要 指定您的代码仅适用于具有特定条件的子类 特性.子类除了继承外不必关联 来自基层.例如:

    This is useful when you have a large inheritance hierarchy, but want to specify that your code works with only subclasses that have certain properties. The subclasses don’t have to be related besides inheriting from the base class. For example:

    class Control {
        private state: any;
    }
    
    interface SelectableControl extends Control {
        select(): void;
    }
    
    class Button extends Control implements SelectableControl {
        select() { }
    }
    
    class TextBox extends Control {
        select() { }
    }
    
    // Error: Property 'state' is missing in type 'Image'.
    class Image implements SelectableControl {
        private state: any;
        select() { }
    }
    
    class Location {
    
    }
    

    所以,

    • extends的意思是-它从其父级获取所有信息
    • 在这种情况下,
    • implements几乎类似于实现接口.子对象可以假装它是父对象...但是没有任何实现
    • extends means - it gets all from its parent
    • implements in this case is almost like implementing an interface. Child object can pretend that it is parent... but it does not get any implementation

    这篇关于TypeScript中的“扩展"和“实现"之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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