当前位置: 首页 > news >正文

C# 委托、事件、特性程序

====================================================

委托和事件

====================================================

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Man man = new Man("小明");

        Roommate[] roommates = {
            new Roommate("小张"),
            new Roommate("小朱"),
            new Roommate("小李"),
        };

        man.action += roommates[0].TakeFood;
        man.action += roommates[1].TakePakage;
        man.action += roommates[2].TakePakage;
        man.action += roommates[2].TakeFood;

        man.Down();
    }
}

public class Man

    public string Name { get; private set; }

    public event Action action = null;

    public Man(string name)
    {
        Name = name;
    }

    public void Down()
    {
        MessageBox.Show(Name + "下楼了");
        if (action != null) 
        { 
            action();
        }
    }
}

public class Roommate

    public string Name { get; private set; }

    public Roommate(string name)
    {
        Name = name;
    }

    public void TakePakage()
    {
        MessageBox.Show(Name + "取包裹。");
    }

    public void TakeFood() 
    {
        MessageBox.Show(Name + "取食物。");
    }
}

====================================================

特性

====================================================

[AttributeUsage(AttributeTargets.Class)]
public sealed class InformationAttribute : Attribute
{
    public string developer;
    public string version;
    public string description;

    public InformationAttribute(string developer, string version, string description)
    {
        this.developer = developer;
        this.version = version;
        this.description = description;
    }
}

[Information("张三","v1.0","测试类1")]
public class TestClass1

    
}

====================================================

泛型委托

====================================================

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Employee[] employees = {
            new Employee("a",600),
            new Employee("b",100),
            new Employee("c",300),
            new Employee("d",800),
            new Employee("e",200),
        };

        Sort<Employee>(employees, Employee.Compare);

        string str="";
        foreach (Employee emp in employees) 
        { 
            str = str + emp.Name+ "--" + emp.Salary +"\n";
        }
        MessageBox.Show(str);
    }

    public static void Sort<T>(T[] data, Func<T, T, bool> compare)
    {
        bool falg = true;
        do 
        {
            falg = false;
            for (int i = 0; i < data.Length-1; i++)
            {
                if (compare(data[i], data[i + 1]))
                {
                    T temp = data[i];
                    data[i] = data[i + 1];
                    data[i + 1] = temp;
                    falg = true;
                }
            }
        } while (falg);
    }
}

public class Employee

    public string Name { get; set; }
    public double Salary { get; set; }

    public Employee(string name, double salary)
    {
        Name = name;
        Salary = salary;
    }

    public static bool Compare(Employee e1, Employee e2)
    {
        return e1.Salary > e2.Salary;
    }
}

http://www.lryc.cn/news/112659.html

相关文章:

  • MapTR论文笔记
  • JS进阶-Day4
  • 【C语言】初阶完结练习题
  • c++类与对象详解
  • I/O 函数/缓存和字节流、占位符、getchar(),putchar()
  • MySQL日期常见的函数
  • Python获取CPU温度
  • 后端整理(MySql)
  • HashSet的详细介绍
  • 【SCI征稿】JCR1区,中科院2区,有关大数据、人工智能、机器学习的应用研究均可
  • 【UE】AI导航,多个导航物体无法走到同一终点问题
  • 途游游戏 x 极狐GitLab “通关” DevOps :单元测试从无到优,覆盖率 0→80%
  • 【云原生】Docker-Compose全方面学习
  • 基于 Redux + TypeScript 实现强类型检查和对 Json 的数据清理
  • HIVE语法优化之Join优化
  • 如何申请境内金融信息服务报备
  • VS code:Task
  • 《Java-SE-第三十章》之哲学家就餐问题
  • 关于接口测试用例设计的一些思考
  • gin和gorm框架安装
  • 今天小编继续给大家分享五款高效的电脑宝藏软件
  • SQL Server数据库如何添加mysql链接服务器(Windows系统)
  • scala连接mysql数据库
  • datax-web登陆时出现账号密码错误
  • Redis 和 MySQL如何保证数据一致性
  • VR虚拟仿真技术在道路桥梁中有哪些具体应用?
  • 如何找到死锁的线程?_java都学什么
  • MFC遍历目录包括子目录下所有文件、特定类型文件
  • Kubernetes 集群calico网络故障排查思路
  • OBS视频视频人物实时扣图方法(四种方式)