一、类成员(又叫实例变量或对象变量):

类成员由static关键字修饰,称为静态变量。

特点:可以在未创建类的对象前就用类名直接调用类成员。

1、类变量:由static关键字修饰的字段。

特点:隶属于类模板(共用) ,直接使用类名.访问。

注意:无法使用对象引用

2、类方法:由static关键字修饰的方法

特点:隶属于类模板,直接使用类名.访问。

注意:(1)无法使用对象引用

        (2)方法中不能使用this和base

 (2)方法中不能使用this和base

举例1:

1、未使用static

脚本1

public class Student : MonoBehaviour
{
    int count;
    public Student()
    {
        count++;
    }
    public int Count { get => count;  }
}

脚本2

public class ProGram : MonoBehaviour
{
    
    void Start()
    {
        Student s1 = new Student();
        Student s2 = new Student();
        Student s3 = new Student();
        Student s4 = new Student();
        Student s5 = new Student();
        print(s5.Count);
    }
}

ProGram输出结果为 1 ,原因每一份对象里都会各自有字段count,每一个字段count都为1

2、使用static

修改脚本1:

public class Student : MonoBehaviour
{
    static int count;
    public Student()
    {
        count++;
    }

    public static int Count { get => count;  }
}

修改脚本2:

public class ProGram : MonoBehaviour
{
    
    void Start()
    {
        Student s1 = new Student();
        Student s2 = new Student();
        Student s3 = new Student();
        Student s4 = new Student();
        Student s5 = new Student();
        print(Student.Count);
    }
}

则ProGram输出结果为5

也就是说静态static实现了多个对象共享同一个变量的方式

举例2:

1、未使用static

脚本1:

public class Role : MonoBehaviour
{
    string[] sks;
    public Role()
    {
        sks = new string[]
        {
            "技能1","技能2"
        };
    }

    public string[] Sks { get => sks; }

    //改变技能名字
    public void ChangeSkill(int index, string newSkill)
    {
        sks[index] = newSkill;
    }
}

脚本2:

public class ProGram : MonoBehaviour
{
    void Start()
    {
        Role r1 = new Role();
        Role r2 = new Role();
        for (int i = 0; i < r1.Sks.Length; i++)
        {
            print("r1:" + r1.Sks[i]);
            print("r2" + r2.Sks[i]);
        }
        r1.ChangeSkill(0, "左勾拳");
        r1.ChangeSkill(1, "直拳");

        for (int i = 0; i < 2; i++)
        {
            print("改变后r1:" + r1.Sks[i]);
            print("改变后r2" + r2.Sks[i]);
        }
    }
}

运行ProGram后结果如图

unityKinematic和Static的区别 unity static_类名

 可见我们在ProGram中修改了技能名字

r1技能1名字被修改了,而r2技能2名字不变;

r1技能2名字不变,r2技能2名字被修改。

那么我们添加static再测试一下

修改脚本1:

2、使用static

public class Role : MonoBehaviour
{
    static string[] sks;
    static Role()                //静态成员应该在静态构造中初始化
    {
        sks = new string[]
        {
            "技能1","技能2"
        };
    }

    public string[] Sks { get => sks; }

    //改变技能名字
    public static void ChangeSkill(int index, string newSkill)
    {
        sks[index] = newSkill;
    }
}

修改脚本2:

public class ProGram : MonoBehaviour
{
    void Start()
    {
        Role r1 = new Role();
        Role r2 = new Role();
        for (int i = 0; i < r1.Sks.Length; i++)
        {
            print("r1:" + r1.Sks[i]);
            print("r2" + r2.Sks[i]);
        }
        Role.ChangeSkill(0, "左勾拳");
        Role.ChangeSkill(1, "直拳");

        for (int i = 0; i < 2; i++)
        {
            print("改变后r1:" + r1.Sks[i]);
            print("改变后r2" + r2.Sks[i]);
        }
    }
}

运行ProGram后结果如图

unityKinematic和Static的区别 unity static_c#_02

可以得出结论:静态static实现了多个对象共享同一个变量的方式(静态的共享性)

图示:

unityKinematic和Static的区别 unity static_c#_03

二、静态构造

1、静态构造必须无参(static 类名(){})    

比较:非静态构造负责初始化成员变量

        静态构造负责初始化类模板

特点:

1、每个类中都有此方法

2、不可被直接调用

3、使用类时自动执行(比如说new一个对象,或者类.方法,只要使用就会自动执行)

4、运行期间只执行一次