您的位置:首页 > 移动开发 > Unity3D

unity3D-游戏/AR/VR在线就业班 蓝鸥C#入门事件学习笔记

2016-12-13 13:27 501 查看
unity3D-游戏/AR/VR在线就业班 蓝鸥C#入门事件学习笔记

事件

Event和delegate的关系就好像字段和属性的关系

Event会限制delegate不能够直接赋值操作,防止将委托替换掉,只能使用+=和-=来绑定或者解除绑定

Event还限定了delegate只能在定义的类中被调用

using System;

namespace Lesson_27
{
    public  delegate void  Something(string  name);

    public  class  Student{
        //Event就是delegate 的“属性”
        public  event Something something;

        
        public  void Do(){
            something (name);
        }
        public  Student(string  name){
            this.name = name;
        }
        private string name;    
    }
    public  class Teacher{
        
        public  void Hungry(){
            
            Student s = new Student ("老王");
            //使用了event之后就不能够直接给委托赋值,必须使用+=,-=来绑定,解除绑定
            s.something += new Something (A);
            //使用了event之后就不能够在Student外调委托
            //s.something ("老张");
            s.Do ();
        }
        public  void  A(string  name){
            Console.WriteLine ("Hello,"+name);
        }
    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            Teacher t = new Teacher ();
            t.Hungry ();

        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  VR c# unity3d 蓝鸥 游戏