为什么静态类需要的成员被声明为静态的?为什么不只是含蓄? [英] Why do members of a static class need to be declared as static? Why isn't it just implicit?

查看:230
本文介绍了为什么静态类需要的成员被声明为静态的?为什么不只是含蓄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,不可能是一个静态类的实例成员,因为该类不可能被实例化。为什么我们需要声明成员为静态的?

Obviously there can't be an instance member on a static class, since that class could never be instantiated. Why do we need to declare members as static?

推荐答案

我经常被问到这样的问题所有的时间。基本的问题归结为在一个关于声明的成员实际上可以由编译器推断应这一事实的明确声明是:(1)要求,(2)可选,或者(3)禁止?

I get asked questions like this all the time. Basically the question boils down to "when a fact about a declared member can be deduced by the compiler should the explicit declaration of that fact be (1) required, (2) optional, or (3) forbidden?"

有没有一个简单的答案。每个人都必须采取逐案的基础。把静的静态类的成员是必需的。把新在一个隐藏,派生类的非首要方法是可选的。把静上一个const是被禁止的。

There's no one easy answer. Each one has to be taken on a case-by-case basis. Putting "static" on a member of a static class is required. Putting "new" on a hiding, non-overriding method of a derived class is optional. Putting "static" on a const is forbidden.

简要考虑你的情况,似乎匪夷所思,使之禁。你有全班充满方法标记为静态。你决定,使类的静态,这意味着你必须删除所有的静态修饰符?这很奇怪。

Briefly considering your scenario, it seems bizarre to make it forbidden. You have a whole class full of methods marked "static". You decide to make the class static and that means you have to remove all the static modifiers? That's weird.

这似乎匪夷所思,使之选;假设你有一个静态类和两种方法,一种标有静,一个人不。由于静电是不正常的默认,他很自然地认为有旨在成为它们之间的区别。使其可选似乎是潜在的混乱。

It seems bizarre to make it optional; suppose you have a static class and two methods, one marked static, one not. Since static is not normally the default, it seems natural to think that there is intended to be a difference between them. Making it optional seems to be potentially confusing.

这使得使得人们必需的,因为最不坏的三个选项。

That leaves making it required, as the least bad of the three options.

请参阅<一href="http://blogs.msdn.com/b/ericlippert/archive/2010/06/10/don-t-repeat-yourself-consts-are-already-static.aspx">http://blogs.msdn.com/b/ericlippert/archive/2010/06/10/don-t-repeat-yourself-consts-are-already-static.aspx关于这些各种各样的问题,更多的想法。

See http://blogs.msdn.com/b/ericlippert/archive/2010/06/10/don-t-repeat-yourself-consts-are-already-static.aspx for more thoughts on these sorts of problems.

这篇关于为什么静态类需要的成员被声明为静态的?为什么不只是含蓄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆