是否可以在Dart的类中将方法声明为final

是否可以在Dart的类中将方法声明为final

本文介绍了是否可以在Dart的类中将方法声明为final?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,可以在类中声明一种方法,以防止子类覆盖它.

In Java one can declare a method within a class to prevent subclasses from overriding it.

例如:

class Foo {

   final void bar() {
     // some code here.
   }

}

Dart中有类似的构造吗?

Is there a similar construction in Dart?

推荐答案

package:meta 提供了 @nonVirtual 注释,以禁止覆盖方法和 @sealed 注释完全禁止派生类.

package:meta provides a @nonVirtual annotation to disallow overriding methods and a @sealed annotation to disallow derived classes entirely.

请注意,这些注释仅向 dartanalyzer 提供提示.它们实际上并不会阻止任何违反注释的操作,而是会在执行分析时导致打印警告.

Note that these annotations just provides hints to dartanalyzer. They won't actually prevent anything from violating the annotations and instead will cause warnings to be printed when analysis is performed.

这篇关于是否可以在Dart的类中将方法声明为final?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:50