Unity C# basic review 01——static static(P267)

1. Class members (also called instance variables or object variables):

Class members are modified by the static keyword and are called static variables.

Features: You can directly call class members using the class name before creating an object of the class.

1. Class variables: fields modified by the static keyword.

Features: It belongs to the class template (shared) and can be accessed directly using the class name.

NOTE: Object references cannot be used

2. Class method: method modified by static keyword

Features: It belongs to the class template and can be accessed directly using the class name.

Note: (1) Object reference cannot be used

        (2) This and base cannot be used in methods

 (2) This and base cannot be used in methods

Example 1:

1. Static is not used

Screenplay 1

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

Screenplay 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);
    }
}

The output result of ProGram is  1, because each object has its own field count, and each field count is 1.

2. Use static

Modify script 1:

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

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

Modify script 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);
    }
}

Then the ProGram output result is 5

In other words, static static implements how multiple objects share the same variable.

Example 2:

1. Static is not used

Script 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;
    }
}

Script 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]);
        }
    }
}

The result after running ProGram is as shown in the figure

 It can be seen that we modified the skill name in ProGram

The name of skill 1 of r1 has been modified, but the name of skill 2 of r2 remains unchanged;

The name of skill 2 of r1 remains unchanged, and the name of skill 2 of r2 is modified.

Then let’s add static and test it again

Modify script 1:

2. Use 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;
    }
}

Modify script 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]);
        }
    }
}

The result after running ProGram is as shown in the figure

It can be concluded that static static realizes the way that multiple objects share the same variable (static sharing)

Illustration:

2. Static structure

1. Static construction must have no parameters (static class name(){})    

Comparison: Non-static construction is responsible for initializing member variables

        Static constructor is responsible for initializing the class template

Features:

1. This method exists in every class

2. Cannot be called directly

3. Automatically execute when using a class (for example, new an object, or a class.method will automatically execute as long as it is used)

4. Execute only once during operation

Guess you like

Origin blog.csdn.net/weixin_46711336/article/details/122946496