禁止实例调用共享/静态方法?

发布于 2024-09-15 23:59:26 字数 237 浏览 5 评论 0原文

是否可以禁止类的实例调用共享/静态方法?

例如:

我想允许这样做:

ClassName.MethodOne()

但我想禁止这样做:

Dim A As New ClassName
A.MethodOne()

之所以需要这样做,是因为在这种情况下,如果实例可以调用该方法,那么在语义上会造成混乱。

Is it possible to prohibit an instance of a class from calling a shared/static method?

For example:

I want to allow this:

ClassName.MethodOne()

But I want to disallow this:

Dim A As New ClassName
A.MethodOne()

The reason this is desirable is that in this case it is semantically confusing if an instance can call the method.

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

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

发布评论

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

评论(2

夏花。依旧 2024-09-22 23:59:26

不,这在 VB.Net 中是不可能的。上面的代码将发出警告(BC42025),但会成功编译。唯一的困难选择是将警告切换为错误,这将防止出现此问题。

No this is not possible in VB.Net. The above code will emit a warning (BC42025) but will sucessfully compile. The only hard option is to switch warnings to errors and that will prevent this problem.

别闹i 2024-09-22 23:59:26

正如所说,这是默认的警告。但有可能将其推向错误。转到 VB.net 项目的属性,然后转到“编译”选项卡,然后转到“警告配置”。找到“实例变量访问共享成员”并将其设置为错误。现在它无法编译。

As it was said it is warning by default. But it is possible to promote it to error. Go to properties of VB.net project, then Compile tab, then Warning configurations. Find there "Instance variable accesses shared member" and set it to Error. Now it will not compile.

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