Unity C# Basic Review 30 - Events (P452)

1. Origin of the incident

class Person 
{
    public void Sleep() 
    {
        Debug.Log("主人在睡觉");
    }
    public void Aweak() 
    {
        Debug.Log("主人醒来");
    }
}

class Cat 
{
    public void Play()
    {
        Debug.Log("猫在晒太阳");
    }
    public void Attack()
    {
        Debug.Log("挠人");
    }
}

class Dog 
{
    public void Play()
    {
        Debug.Log("狗在捉耗子");
    }
    public void Attack()
    {
        Debug.Log("咬人");
    }
}

delegate void Alarm();
class Home 
{
    Person p;
    Cat c;
    Dog d;
    public Alarm alarm;

    public Home(Person p, Cat c, Dog d)
    {
        this.p = p;
        this.c = c;
        this.d = d;
    }

    internal Person P { get => p; set => p = value; }
    internal Cat C { get => c; set => c = value; }
    internal Dog D { get => d; set => d = value; }

    public void Alive(string action) 
    {
        if (action == "平静生活")
        {
            p.Sleep();
            c.Play();
            d.Play();
        }
        else 
        {
            if (alarm != null) 
            {
                alarm();

            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Control : MonoBehaviour
{
    void Start()
    {
        Person p = new Person();
        Cat c = new Cat();
        Dog d = new Dog();
        Home home = new Home(p, c, d);
        home.alarm = c.Attack;
        home.alarm += d.Attack;
        home.alarm += p.Aweak;

        home.Alive("平静生活!");
        //home.Alive("进小偷啦!");
    }

}

2. Add the event delegate to change the event (only += and -= can be used. After decompilation, you can see that there are only Add and Remove methods)

1. Events are special delegates, which provide encapsulation for delegates. On the one hand, they allow adding and deleting bound methods from outside the class, but on the other hand, they do not allow the bound methods to be delegated from outside the class.

2. An event can be regarded as a special kind of delegation. It implicitly establishes two methods, Add and Remove, for the delegation object, which are used as processing methods for registration and cancellation events. Moreover, the variable members corresponding to the event will be regarded as private variables, and the outside world cannot directly access them beyond the object where the event is located. This makes the event have good encapsulation and eliminates the cumbersome code such as Add and Remove.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


class Person 
{
    public void Sleep() 
    {
        Debug.Log("主人在睡觉");
    }
    public void Aweak() 
    {
        Debug.Log("主人醒来");
    }
}

class Cat 
{
    public void Play()
    {
        Debug.Log("猫在晒太阳");
    }
    public void Attack()
    {
        Debug.Log("挠人");
    }
}

class Dog 
{
    public void Play()
    {
        Debug.Log("狗在捉耗子");
    }
    public void Attack()
    {
        Debug.Log("咬人");
    }
}

delegate void Alarm();
class Home 
{
    Person p;
    Cat c;
    Dog d;
    public event Alarm alarm;  //加上event叫事件

    public Home(Person p, Cat c, Dog d)
    {
        this.p = p;
        this.c = c;
        this.d = d;
    }

    internal Person P { get => p; set => p = value; }
    internal Cat C { get => c; set => c = value; }
    internal Dog D { get => d; set => d = value; }

    public void Alive(string action) 
    {
        if (action == "平静生活")
        {
            p.Sleep();
            c.Play();
            d.Play();
        }
        else
        {
            if (alarm != null)
            {
                alarm();
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Control : MonoBehaviour
{
    void Start()
    {
        Person p = new Person();
        Cat c = new Cat();
        Dog d = new Dog();
        Home home = new Home(p, c, d);
        home.alarm += c.Attack;
        home.alarm += d.Attack;
        home.alarm += p.Aweak;

        home.Alive("平静生活!");
        //home.Alive("进小偷啦!");
        //home.alarm();   //不希望alarm()在外部执行,则给委托加上event
    }

}

Guess you like

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