“公共静态”与“静态公共” - 有什么区别吗?
sealed class PI
{
public static float number;
static PI()
{ number = 3.141592653F; }
static public float val()
{ return number; }
}
公共静态和静态公共有什么区别?它们可以按任意顺序使用吗?
如何使用
static public float val()
?类初始化后会立即执行吗?
sealed class PI
{
public static float number;
static PI()
{ number = 3.141592653F; }
static public float val()
{ return number; }
}
What's the difference between public static and static public? Can they be used in any order?
How would I use
static public float val()
?Does it get executed as soon as the class is initialized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有什么区别。您可以按任意顺序指定它们。
然而,我发现大多数开发人员倾向于使用:
而不是 static public。
There's no difference. You're free to specify them in either order.
However, I find that most developers tend to use:
and NOT static public.
关于修饰符的顺序
它们可以按任何顺序使用。这只是您使用哪种风格的选择。我总是首先使用可见性,大多数其他代码也是如此。
关于第二个问题:
这只是一个静态函数。您可以使用 PI.val() 来调用它。您只是不需要类的实例来调用它,而是直接在类上调用它。静态函数不接收
this
引用,不能是虚拟的,它就像非 OOP 语言中的函数一样,只不过它使用类作为命名空间。About the ordering of modifiers
They can be used in any order. It's just a stylistic choice which one you use. I always use visibility first, and most other code does too.
About the second question:
This is just a static function. You call it with
PI.val()
. You just don't need an instance of the class to call it, but call it on the class directly. A static function does not receive athis
reference, can't be virtual, it's just like a function in a non OOP language, except that it's using the class as namespace.嗯,它就像一个人的名字=)
叫汤姆·迈克或迈克·汤姆,没有区别。
Well, it is just like the name of a Person =)
Calling Tom Mike or Mike Tom, no difference.
没有什么区别。它们的顺序相对于彼此而言并不重要
There is no difference. Their order is not important with respect to each other
要回答您的第二个问题,它可能应该写为
并这样称呼它:
编写这样一个类的原因是缓存该值,从而节省后续调用该方法的时间。如果获取 pi 的方法需要大量时间来执行计算,那么您只想计算一次。
To answer your second question, it should probably be written as
And call it thusly:
The reason for writing such a class is to cache the value, saving time on subsequent calls to the method. If the way to get pi required a lot of time to perform the calculations, you would only want to do the calculations once.
关于第二个问题:该方法无需类实例即可使用,可以这样调用:
因为该类只有静态成员,所以该类可能应该是静态类,然后它永远无法初始化。
With regards to the second question: The method is available without an instance of a class, it could be called thusly:
Because the class only has static members, the class should probably be a static class, and then it could never get initialized.