首页 > 解决方案 > 在 c# 中使用静态类与编译器方式的过程编程相同吗?

问题描述

基本上我使用的是 Unity,它使用了 c#。我想按程序编程。我可以用静态命名空间和静态类来模仿它,这样我就可以将静态命名空间用作“头文件”,这样我就不必实例化一堆单例了。这种表现如何明智?

例如:

namespace Balls 
{
    public static Ball [] balls;

    public static class Balls
    {
        public static void UpdateBalls (){}
    }  
}

另一个文件:

using static Balls;

namespace Player 
{
    public static class Player 
    {
        public static bool PlayerAlive;
        public static Vector3 position;

        public static void UpdatePlayer () 
        {
            for (int i=0; i<balls.Length; i++)
            {
                if (position == balls[i].position)
                {
                    PlayerAlive = false;
                }
            }
        }
    }
}

另一个文件:

using static Balls;
using static Player;

namespace Main 
{
    public static class Main 
    {
        public static void MainUpdate ()
        {
            if (PlayerAlive) 
            {
                UpdateBalls ();
                UpdatePlayer ();
            } 
        }
    }
}

标签: c#staticnamespacesprocedural

解决方案


推荐阅读