委托例题

来源:互联网 发布:linux 如何查看用户数 编辑:程序博客网 时间:2024/06/05 01:16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
    public delegate void Something(string name);
    public class student
    {
        public void Do(Something something)
        {
            something(name);
        }
        public student(string name)
        {
            this.name = name;
        }
        private string name;
        
        
    }
    public class teacher
    {
        public void hungry()
        {
            student s = new student("老王");
            Something a = A;
            s.Do(a);
        }
        public void A(string name)
        {
            Console.WriteLine("hello"+name);
        }
    }
    /*1.创建一个student类,在类中创建一个私有的name属性,再创建一个公共的构造函数,并把触发name
2.在student类中创建一个公共的do方法
3.创建一个teacher类,在类中创建一个公共的hungry方法,并在方法中实例化一个student
4.在hungry方法中调用do方法
//定义委托 - 访问修饰符 delegate 返回值类型 委托名(参数列表);
5.定义一个委托,名叫something,在do方法中使用something委托
6.在teacher中创建委托变量,something a = new something(已实现的方法名);
7.在teacher中创建一个与委托返回值和变量相同的方法*/
    class Program
    {
        static void Main(string[] args)
        {
            teacher t = new teacher();
            t.hungry();
        }
    }
}
原创粉丝点击