打字稿抽象属性 [英] Typescript abstract property

查看:105
本文介绍了打字稿抽象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我开始学习打字稿。我知道所有主要的OOP概念,但我只是不了解抽象属性背后的概念。我知道您必须重写/实现子类中基类的抽象成员。但是,它的用途是什么?我得到抽象方法背后的概念,但事实并非如此。
如果您能给我介绍一些很好的例子,我将不胜感激。

I started learning typescript a few days ago. I know all the major OOP concepts, but I just don't understand the concept behind abstract properties. I understand that you have to override/implement the abstract members of your base class in the child class. But still, what is it used for? I get the concept behind abstract methods, but not this. If you could present me some nice examples, I would really appreciate it.

谢谢!

推荐答案

抽象属性之所以有用,与抽象方法的使用类似。 readonly属性在概念上与getter方法在用途上相似,因此抽象的readonly属性有点像具有抽象的getter方法。

Abstract properties are useful for similar reasons that abstract methods are; a readonly property is conceptually similar in purpose to a getter method, so an abstract readonly property is a bit like having an abstract getter method.

例如,假设您有一个用于表示表达式的树结构:您可能有一个用于二进制表达式的抽象类,为避免重复, toString 方法可能要使用 this.op 字符串属性,用于在字符串表示形式中使用的适当符号(例如'+')。下面的代码显示了可能的层次结构中的两个类:

For one example, imagine you have a tree structure for representing expressions: you might have an abstract class for binary expressions, and to avoid duplication, the toString method might want to make use of a this.op string property for the appropriate symbol to use in the string representation (e.g. '+'). The code below shows two classes in a possible hierarchy:

abstract class MyBinaryExpr extends MyExpr {
    constructor(readonly left: MyExpr, readonly right: MyExpr) { super(); }

    abstract readonly op: string;
    toString(): string {
        return '(' + this.left + this.op + this.right + ')';
    }
}

class MyAdd extends MyBinaryExpr {
    op = '+';
    compute(): number {
        return this.left.compute() + this.right.compute();
    }
}

如果使用类似Java的语言编写了相同的代码在属性不能抽象的地方, MyBinaryExpr 类可能具有类似 abstract String getOp()的方法。

If the same code were written in a language like Java where properties cannot be abstract, the MyBinaryExpr class would probably have a method like abstract String getOp() for the same purpose.

与Java相比,另一件值得注意的事情是在Java中,只有抽象方法才有意义,因为方法调用Java中的在运行时动态分配到属于该对象类的具体方法。当某些代码调用抽象方法时,该调用(通常)不能在编译时绑定到具体实现,因此必须在运行时选择具体方法。

Another thing worth noting specifically in comparison to Java is that in Java, having abstract methods only makes sense because method calls in Java are dynamically dispatched to the concrete method belonging to the object's class at runtime. When some code calls an abstract method, the call cannot (in general) be bound to a concrete implementation at compile-time, so the concrete method must be selected at runtime.

另一方面,Java中的 obj.field 之类的字段访问为 obj 的编译时类型,在编译时nofollow noreferrer>静态绑定到属于一个类的字段声明。因此,Java接口不能具有抽象字段,因为编译器在编译时不知道要绑定到哪个实际字段声明。因此,Java的语义不允许抽象字段。另一方面,Javascript(进而是Typescript)在运行时解析所有成员访问,因此,甚至属性访问也被动态绑定。因此,语义允许接口具有抽象属性。

On the other hand, field accesses like obj.field in Java are statically bound at compile-time to the field declaration belonging to a class according to the compile-time type of the expression obj. So a Java interface cannot have abstract fields because the compiler wouldn't know which actual field declaration to bind to at compile-time. So Java's semantics do not allow for abstract fields. On the other hand, Javascript (and hence Typescript) resolve all member accesses at runtime, so even property accesses are dynamically bound. Hence the semantics allow for interfaces to have abstract properties.

这篇关于打字稿抽象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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