文档02_面向对象

面向对象学习
此思想需要自行领悟!
关于委托
委托是一种类,将委托的方法执行,参数即方法传递的参数

class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            weituo d = p.ch;//简单委托
            d("123");
            Console.ReadKey();
        }
        public delegate void weituo(string str);
        public void ch(string str)
        {
            Console.WriteLine("委托 "+str );
        }
    }


复杂

class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            weituo d = new weituo(p.ch);//初始赋值委托
            d += p.ch;//
            d += p.chc;
            d("123");
            p.wt("456", new weituo(delegate(string str) { Console.WriteLine("匿名 委托 " + str); }));
            //委托必须要赋值,封装命名方法或者匿名方法
            Console.ReadKey();
        }
        public delegate void weituo(string str);
        public int b=11;
        public void ch(string str)
        {
            Console.WriteLine("ch 委托 "+str );
        }
        public void chc(string str)
        {
            Console.WriteLine("chc 委托 " + str);
        }
        public void wt(string value,weituo wei)
        {
            wei(value);
        }
    }


深入

class UserClass
    {
        public string name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            weituo d = new weituo(p.ch);//初始赋值委托
            d += p.ch;//多播委托
            d += p.chc;
            d("123");
            d("852");
            //匿名委托函数
            p.wt("456", new weituo(delegate(string str) { Console.WriteLine("匿名 委托 " + str); }));

            weituo<Program> w = new weituo<Program>(p.wtfx);
            w("ppppp",p);
            UserClass u = new UserClass() { name = "lww" };
            weituo<UserClass> wi = new weituo<UserClass>(p.uwtfx);
            wi("value",u);
            //委托必须要赋值,封装命名方法或者匿名方法
            //它的参数值是符合参数类型的方法
            Console.ReadKey();
        }
        public delegate void weituo(string str);
        //泛型委托
        public delegate void weituo<T>(string str,T t);
        public int b=11;
        public void ch(string str)
        {
            Console.WriteLine("ch 委托 "+str );
        }
        public void chc(string str)
        {
            Console.WriteLine("chc 委托 " + str);
        }

        public void wt(string value,weituo wei)
        {
            wei(value);
        }
        //泛型委托
        public void wtfx(string value, Program p)
        {
            //Console.WriteLine("委托 " + value);
            p.ch(value);
        }
        //泛型委托
        public void uwtfx(string value, UserClass u)
        {
            Console.WriteLine(value+"委托 " + u.name);
        }
    }


关于委托的返回值:如果委托中有返回值并且在调用列表中有一个以上的方法,调用列表中最后一个方法返回的值就是委托调用返回的值,调用列表中所有其他方法的返回值都会被忽略
ref 与 out,在初始时ref的参数传递之前必须赋有初值,out则不用



关于事件(event)
在窗体中拖动一个button控件
 常用创建事件
 
 
在配置文件Form1.Designer.cs中注册了事件

/*事件接收发送初探*/

public delegate void MyEventHander(object sender, MyEventArgs e);/*定义委托*/
    public partial class Form1 : Form
    {
        public static event MyEventHander Action;/*定义事件*/
        public Form1()
        {
            InitializeComponent();
            //通过委托建立事件
            //public delegate void EventHandler(object sender, EventArgs e);
            button1.Click += new EventHandler(delegate(object o, EventArgs a) /*匿名创建*/
                {
                    button1.Text = "***制造";
                });
            button1.Click += Lw;/*累加创建*/
             
        }

        private void Lw(object o, EventArgs e)
        {
            UserEvent u=new UserEvent();//这个类相当于form1,在其中注册了事件
            MyEventArgs Mye=new MyEventArgs();
            /*理解01*/
            Form1.Action(this,Mye);
            button1.Text = u.value;
            /*理解02*/
            u.MyActionEvent += u_MyActionEvent;/*窗体事件原理*/

        }
        private void u_MyActionEvent(object sender, MyEventArgs e)
        {
            e.Message = "998";/*自定义*/
         button1.Text = e.Message;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*常用普通形式*/
        }
        
    }

    /*消息类*/
    public class MyEventArgs :EventArgs
    {
        public string Message { get; set; }
    }
    public class UserEvent
    {
        /*申明事件*/
        public event MyEventHander MyActionEvent;
        public UserEvent()
        {
            Form1.Action+=new MyEventHander(OnAction);
        }
        /*在类中创建*/
        public void OnAction(object sender,MyEventArgs e)
        {
            e.Message="123456789";
            value=e.Message;
        }
        public string value{get;set;}
    }







因缘际会的相遇,自当有非同寻常的结局 QQ交流群:110826636
原文地址:https://www.cnblogs.com/RainbowInTheSky/p/3044013.html