简体   繁体   English

将@override放在我的PolymerDart方法中的目的是什么,什么时候没有?

[英]What is the purpose of putting @override in my PolymerDart methods, when it works without?

In terms of creating classes which involve inheritance, some actually need the annotation of override. 在创建涉及继承的类方面,有些实际上需要覆盖的注释。 When working with PolymerDart though, specifically 特别是在使用PolymerDart时

@override
attached(){...}

I see no reason why i need it. 我认为没有理由需要它。 It works as expected without it. 没有它,它按预期工作。

Since it is just an annotation, is it just for Developers to see so they understand that function is overriding some other function? 因为它只是一个注释,它是否只是让开发人员看到它们才能理解该函数是否覆盖了其他一些函数?

My bet is that it is just for developers, and unlike other annotations which carry out some sort of execution, these do not. 我敢打赌,它只适用于开发人员,与其他执行某种执行的注释不同,这些不是。

If you enable the linter rule annotate_overrides the DartAnalyzer provides hints 如果启用了棉短绒规则annotate_overrides的DartAnalyzer提供线索

  • for members that override other members of superclasses or interfaces, but don't have the @override annotation 对于覆盖其他超类或接口成员但没有@override注释的成员
  • for members that have the @override annotation, but don't actually override other members of superclasses or interfaces. 对于具有@override注释的成员,但实际上不覆盖超类或接口的其他成员。

The @override annotation is helpful if it is used consistently, because it shows when a member is an override. @override注释在一致使用时很有用,因为它显示成员何时是覆盖。
The linter rule ensures that it is used consistently. linter规则确保一致地使用它。

There is also the ` overridden_fields lint, but AFAIK this is going to be deprecated because. 还有` overridden_​​fields lint,但是AFAIK将被弃用,因为。 This was forbidden until recently in Dart Development compiler but this restriction was removed. 直到最近才在Dart Development编译器中禁止此操作,但此限制已被删除。 I don't know if it is still discouraged, but there are cases where it makes sense and therefore I think the linter rule shouldn't be used anymore because it is prone to cause hints for valid code. 我不知道它是否仍然气馁,但有些情况下它是有道理的,因此我认为不应再使用linter规则,因为它很容易引起有效代码的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM