gpt4 book ai didi

oop - 允许在非抽象类中声明抽象方法的目的是什么?

转载 作者:行者123 更新时间:2023-12-04 08:47:33 25 4
gpt4 key购买 nike

根据 this article ,在 Dart 中,可以定义一个非抽象类来具有抽象(或未实现)方法。抽象方法会导致警告,但不会阻止实例化。

允许在 Dart 的非抽象(或具体)类中声明抽象方法的目的是什么?为什么 Dart 被设计为以这种方式工作?

最佳答案

specification在具体类中声明抽象方法实际上非常明确:

It is a static warning if an abstract member m is declared or inherited in a concrete class


We wish to warn if one declares a concrete class with abstract members.


It is a static warning if a concrete class has an abstract member (declared or inherited).



他们没有任何预期用途 ,这就是他们发出警告的原因。如果您熟悉 Java:它类似于 accessing a static member via an object ,这也是毫无意义的并触发警告。

至于为什么能通过编译,Dart 使用了一个 optional type system ,这意味着输入概念不应影响语言的语义,而这正是 Dart 所强制执行的:

The purpose of an abstract method is to provide a declaration for purposes such as type checking and reflection.


The static checker will report some violations of the type rules, but such violations do not abort compilation or preclude execution.

关于oop - 允许在非抽象类中声明抽象方法的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45738028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com