“公共静态”与“静态公共” - 有什么区别吗?

发布于 2024-10-01 10:48:51 字数 305 浏览 4 评论 0原文

sealed class PI
{
  public static float number;
  static PI()
  { number = 3.141592653F; }
  static public float val()
  { return number; }
}
  1. 公共静态和静态公共有什么区别?它们可以按任意顺序使用吗?

  2. 如何使用static public float val()

    类初始化后会立即执行吗?

sealed class PI
{
  public static float number;
  static PI()
  { number = 3.141592653F; }
  static public float val()
  { return number; }
}
  1. What's the difference between public static and static public? Can they be used in any order?

  2. How would I use static public float val()?

    Does it get executed as soon as the class is initialized?

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

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

发布评论

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

评论(6

浮生未歇 2024-10-08 10:48:51

没有什么区别。您可以按任意顺序指定它们。
然而,我发现大多数开发人员倾向于使用:

public static

而不是 static public。

There's no difference. You're free to specify them in either order.
However, I find that most developers tend to use:

public static

and NOT static public.

赠我空喜 2024-10-08 10:48:51

关于修饰符的顺序

它们可以按任何顺序使用。这只是您使用哪种风格的选择。我总是首先使用可见性,大多数其他代码也是如此。

关于第二个问题:

static public float val()

这只是一个静态函数。您可以使用 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:

static public float val()

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 a this 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.

初见你 2024-10-08 10:48:51

嗯,它就像一个人的名字=)
叫汤姆·迈克或迈克·汤姆,没有区别。

Well, it is just like the name of a Person =)
Calling Tom Mike or Mike Tom, no difference.

独享拥抱 2024-10-08 10:48:51

没有什么区别。它们的顺序相对于彼此而言并不重要

There is no difference. Their order is not important with respect to each other

太阳男子 2024-10-08 10:48:51

要回答您的第二个问题,它可能应该写为

public static class Pi
{
    private static float pi = 0;

    public static float GetValue()
    {
        if (pi == 0)
            pi = 3.141592653F;   // Expensive pi calculation goes here.

        return pi;
    }
}

并这样称呼它:

float myPi = Pi.GetValue();

编写这样一个类的原因是缓存该值,从而节省后续调用该方法的时间。如果获取 pi 的方法需要大量时间来执行计算,那么您只想计算一次。

To answer your second question, it should probably be written as

public static class Pi
{
    private static float pi = 0;

    public static float GetValue()
    {
        if (pi == 0)
            pi = 3.141592653F;   // Expensive pi calculation goes here.

        return pi;
    }
}

And call it thusly:

float myPi = Pi.GetValue();

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.

怼怹恏 2024-10-08 10:48:51

关于第二个问题:该方法无需类实例即可使用,可以这样调用:

PI.val();

因为该类只有静态成员,所以该类可能应该是静态类,然后它永远无法初始化。

With regards to the second question: The method is available without an instance of a class, it could be called thusly:

PI.val();

Because the class only has static members, the class should probably be a static class, and then it could never get initialized.

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