C#笔记(十九)——委托和事件
Posted 无畏先锋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#笔记(十九)——委托和事件相关的知识,希望对你有一定的参考价值。
课堂练习
using System;
using System.Collections.Generic;
namespace Lesson13_3
{
//张三:初版按钮的实现
/*class Button
{
public string BtnName { get; set;}
public Button(string btnName){
BtnName = btnName;
}
public void Onclick(){
Console.WriteLine (BtnName+ ":被点击了");
if (BtnName == "登录") {
LoginPageLogic logic = new LoginPageLogic ();
logic.Login ();
} else if (BtnName == "取消") {
LoginPageLogic logic = new LoginPageLogic ();
logic.CancelLogin ();
}
}
public void Onclick(Hero h){
if (BtnName == "a1") {
h.LeftAction ();
}
}
}*/
//张三:委托版按钮的实现
class Button
{
//定义一个按钮委托
public delegate void OnclickHandler ();
//定义一个委托变量,用来存放点击操作的具体方法
public OnclickHandler oncliAction;
public void AddOnclickAction(OnclickHandler param){
oncliAction = param;
}
public string BtnName { get; set;}
public Button(string btnName){
BtnName = btnName;
}
//按钮点击的时候调用的方法
public void Onclick(Action< string> callBack){
Console.WriteLine (BtnName+ ":被点击了" );
if (oncliAction != null ) {
//执行委托
oncliAction ();
}
if (callBack != null ) {
callBack (BtnName);
}
}
}
//李四:登录页面逻辑
class LoginPageLogic {
public void Login(){
Console.WriteLine ("登录成功" );
}
public void CancelLogin(){
Console.WriteLine ("取消登录" );
}
}
//王二: 英雄类
class Hero
{
public string HeroName{ get; set;}
public void LeftAction(){
Console.WriteLine (HeroName+" 正在犀利的向左走位" );
}
public void RightAction(){
Console.WriteLine (HeroName+" 正在犀利的向右走位" );
}
}
class PrintTool {
//定义一个判断字符串是否相等的方法委托
public delegate bool StringIsEquals( string str1,string str2);
public static void PrintIntArr( int[] arr){
foreach (int item in arr) {
Console.WriteLine (item);
}
}
//判断两个字符串是否相等的方法
public bool ArrayisEquals(string str1, string str2){
/*if (str1 == str2) {
return true;
} else
return false;*/
return str1 == str2;
}
}
class MainClass
{
public static void Main ( string[] args)
{
//委托的定义格式:
//访问修饰符 delegate 返回值类型 委托名称 (参数列表)
//访问修饰符 返回值类型 方法名称 (参数列表)
//委托的用法:
//1、声明委托
VoidFunction action;
//2、绑定委托
// 1.直接赋值
//action = EngGreeting;
// 2.通过new关键字来初始化绑定
action = new VoidFunction (EngGreeting);
//3、调用(和方法的调用类似)
//action ("张三");
//4、多播委托,可以将多个方法通过+=运算符绑定到一个委托上
// 调用委托的时候,会将委托上绑定的方法分别执行
action += ChineseGreeting;
action ("李四" );
//1、绑定实例对象的方法
/*PrintTool print = new PrintTool ();
//声明委托
Print printArr = new Print (print.PrintIntArr);
int[] result = new int[6]{ 1,2,3,4,5,6};
printArr (result);*/
//2、
int[] arr = new int[5]{ 1,2,3,4,5};
Print printArr = new Print ( PrintTool.PrintIntArr);
printArr (arr);
PrintTool tool = new PrintTool ();
PrintTool.StringIsEquals isEquals = new PrintTool. StringIsEquals (tool.ArrayisEquals);
bool result = isEquals ("a" ,"a");
Console.WriteLine (result);
List<int > arrlist = new List< int> ();
arrlist.Add (2);
arrlist.Add (1);
arrlist.Add (5);
arrlist.Add (3);
arrlist.Sort ();
foreach (var item in arrlist) {
Console.WriteLine (item);
}
Console.Clear ();
//合并委托: 如果委托有返回值,那么返回值就是最后绑定的方法的返回值
Addelegate del = Add;
del += Mul;
int result1 = del (2,3);
Console.WriteLine (result1);
//解除绑定:注意委托不能为null
del -= Mul;
del -= Mul;
//del -= Add;
Console.WriteLine (del(2,3));
//登录,取消页面逻辑
LoginPageLogic page = new LoginPageLogic ();
//登录按钮
Button loginBtn = new Button ( "登录");
//为登录按钮绑定登录逻辑
loginBtn.AddOnclickAction (page.Login);
loginBtn.Onclick (delegate(string message){
Console.WriteLine (message);
});
//为取消按钮绑定取消登录的逻辑
Button cancelBtn = new Button ( "取消");
cancelBtn.oncliAction = page.CancelLogin;
cancelBtn.Onclick (null);
Hero dema = new Hero ();
dema.HeroName = "德玛西亚" ;
//向左操作按钮
Button leftBtn = new Button ( "a1");
leftBtn.oncliAction = dema.LeftAction;
leftBtn.Onclick (null);
//s向左操作按钮
Button rightBtn = new Button ( "d1");
rightBtn.oncliAction = dema.RightAction;
//匿名方法1、无返回值的匿名方法:delegate(参数列表){ 方法体 }2、有返回值的匿名方法:delegate(int x,int y){return x+y}3、在将匿名方法赋值给委托的时候,末尾的 ; 不能忘
Addelegate delAdd = delegate (int x, int y) {
return x + y;
};
//2、lambda表达式:
delAdd = (int x,int y) => {
return x*y;
};
Console.WriteLine (delAdd (4,5));
//Func:范型委托
Func<int ,int ,string, float> chu = (int x,int y,string name) => {
Console.WriteLine (name);
return x+y;
};
Console.WriteLine (chu(3,5,"张三" ));
}
//定义打印数组的委托
public delegate int Addelegate( int x,int y);
public delegate void Print(int[] arr);
public delegate void VoidFunction( string _n);
public static void ChineseGreeting( string name){
Console.WriteLine ("早上好:" +name);
}
public static void EngGreeting( string name){
Console.WriteLine ("good morning:" +name);
}
public static int Add( int x,int y){
return x + y;
}
public static int Mul( int x,int y){
return x * y;
}
}
}
//================================================================
using System;
namespace Lesson13_2
{
public class Person{
public string name;
public void Congratulate(){
Console.WriteLine (this .name+" 祝贺新郎新娘百年好合~!" );
}
}
public class Button{
public string btnName;
//事件的
public delegate void OnclickHandler();
public event OnclickHandler ClickEvent;
public void AddClickListener(OnclickHandler clickCall){
ClickEvent += new OnclickHandler (clickCall);
}
public void Onclick(){
if (ClickEvent != null ) {
ClickEvent ();
}
}
}
public class CJBoy{
public delegate void MarryHandle();
//事件的发起者
public event MarryHandle marryEvent;
public void SayLove(string qs){
Console.WriteLine ("黑负泪" );
}
public void XYQ(){
Console.WriteLine ("送玫瑰" );
}
public void Song(){
Console.WriteLine ("你笑的多甜蜜蜜蜜" );
}
public bool Ask(string name){
Console.WriteLine ("你到底接不接受" );
return true ;
}
public long PhoneNum(){
return 133010556;
}
public void SendMessage(Action callback){
if (callback != null ) {
callback ();
}
}
//给事件添加一个监听
public void AddMarryListener(MarryHandle listener){
marryEvent += new MarryHandle (listener);
}
public void Marry(){
Console.WriteLine ("要结婚了" );
//事件触发
if (marryEvent != null ) {
marryEvent ();
}
}
}
//delegate bool AskHandle();
//delegate bool AskHandle(string name);
class MainClass
{
public static void Main ( string[] args)
{
CJBoy boy = new CJBoy ();
//Func:用于创建有返回值类型的委托,
//注意,返回值一定义是最后那个参数
Func<string ,bool> laowang
= new Func <string, bool> (boy.Ask);
bool result = laowang ("超级男孩" );
Console.WriteLine (result);
Func<long > laowang2 = new Func< long> (boy.PhoneNum);
Console.WriteLine (laowang2 ());
//Action: 用于创建没有返回值类型的委托
Action<string > laowang3
= new Action <string> (boy.SayLove);
laowang3 ("流浪" );
Action laowang4 = new Action (boy.Song);
laowang4 ();
boy.SendMessage (delegate(){
Console.WriteLine ("吃饭了吗~!" );
});
Person plaowang = new Person();
plaowang.name = "老王" ;
Person p2 = new Person();
p2.name = "老张" ;
Person p3 = new Person();
p3.name = "老李" ;
boy.AddMarryListener (plaowang.Congratulate);
boy.AddMarryListener (p2.Congratulate);
boy.AddMarryListener (p3.Congratulate);
//结婚当天
boy.Marry ();
//
Button loginBtn = new Button ();
loginBtn.AddClickListener (delegate(){
Console.WriteLine ("登陆" );
});
Button cancelBtn = new Button ();
cancelBtn.AddClickListener (delegate(){
Console.WriteLine ("取消登陆" );
});
Button skill1Btn = new Button ();
skill1Btn.AddClickListener (delegate(){
Console.WriteLine ("大招" );
});
loginBtn.Onclick ();
cancelBtn.Onclick ();
skill1Btn.Onclick ();
}
}
}
以上是关于C#笔记(十九)——委托和事件的主要内容,如果未能解决你的问题,请参考以下文章