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

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

问题描述

我几天前开始学习打字稿.我知道所有主要的 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 方法可能想要使用 toStringcode>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 的编译时类型,在编译时绑定到属于一个类的字段声明.所以 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天全站免登陆