@protected是否有@friend功能?

发布于 2025-02-13 06:24:51 字数 656 浏览 0 评论 0原文

我正在使用DART注释@protected,因为我想将成员限制为仅从单个其他类中调用,例如,

class Foo {
  @protected
  void doSomethingSpecial() {}
}

class Bar {
  final foo = Foo();
  consumeDoSomethingSpecial() {
    foo.doSomethingSpecial();
  }
}

可以理解的是,foo.dosomethingspecial() triggers 在C ++世界中,我会注释

    The member 'doSomethingSpecial' can only be used within instance members of subclasses...

栏作为foo的friend允许此通话,但我没有看到DART中的等效注释吗?

添加上方来抑制警告,但是我没有看到适用于使用@protected注释的皮棉规则吗?

// ignore: the_appropriate_lint_rule

我确实看到我可以通过在警告上

I'm using the Dart annotation @protected because I want to limit a member to only be called from a single other class, like so

class Foo {
  @protected
  void doSomethingSpecial() {}
}

class Bar {
  final foo = Foo();
  consumeDoSomethingSpecial() {
    foo.doSomethingSpecial();
  }
}

Understandably, foo.doSomethingSpecial() triggers the warning

    The member 'doSomethingSpecial' can only be used within instance members of subclasses...

In the C++ world I would annotate Bar as a friend of Foo to permit this call but I'm not seeing the equivalent annotation in Dart?

I did see that I can suppress a warning by adding

// ignore: the_appropriate_lint_rule

above the line with the warning but I'm not seeing a lint rule that applies to using the @protected annotation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2025-02-20 06:24:51

DART中的“朋友”基本上是通过确保标识符为“图书馆 - 局部”(从下划线开始)来实现的,并且所有朋友都在同一库中。 DART中的库通常只是一个文件。但是,使用部分/部分可以跨越多个文件。

任何对下划线假定标识符的引用是同一库的范围内,但即使您导入该文件,也不在其他任何地方的范围。

"friend" in Dart is essentially implemented by ensuring that the identifiers are "library-local" (begin with underscore), and all friends are in the same library. A library in Dart is typically just a single file; however, using part/part-of, it can span multiple files.

Any reference to an underscore-prefixed identifier is in-scope for the same library, but out of scope anywhere else, even if you import that file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文