Unity C# 基础复习30——事件(P452)

一、事件的由来

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("进小偷啦!");
    }

}

二、加上event  委托变事件(只能使用+=和-=,反编译之后可以看到只有Add和Remove方法)

1、事件是特殊的委托,他为委托提供了封装性,一方面允许从类的外部增加、删除绑定方法,另一方面又不允许从类的外部来出发委托所绑定方法

2、事件(event)可被视作为一种特殊的委托,它为委托对象隐式地建立起 Add 、Remove 两种方法,用作注册与注销事件的处理方法。而且事件对应的变量成员将会被视为private变量,外界无法超越事件所在对象直接访问它们,这个使事件具备良好的封装性,而且免除了Add 和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
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_46711336/article/details/125528307