您的位置:首页 > 编程语言

委托 (代码复习)

2016-01-01 11:15 183 查看
namespace DelegateDemo
{
//声明委托
public delegate void MyDel();
//声明带参的委托
public delegate void MyDelAdd(int num1, int num2);
//声明带有返值的委托
public delegate string MyDelStrUp(string s);

//声明委托用于演示匿名方法
public delegate string ProcessString(string s);

class Program
{
static void Main(string[] args)
{
#region 委托演示
TestDel t = new TestDel();

#region 简单实例化委托与调用委托
Console.WriteLine("-----以下是简单使用委托演示--------");
//t.MyMethod();
///实例化委托,用一个方法来进行实例化
///该方法签名要与委托签名一致
MyDel del = new MyDel(t.MyMethod);

///调用委托
del();

//C#2.0后可以这种方式实例化委托
MyDel del4 = t.MyMethod;
del4();

//用静态方法进行实例化
del4 = TestDel.MyStaticMethod;
del4();

//以下代码效果相同
// MyDelAdd del2 = new MyDelAdd(t.MyMethod);
//del2(10, 20);
MyDelAdd del2 = t.MyMethod;
del2(10, 20);

//MyDel3 del3 = new MyDel3(t.MyMethod);
//Console.WriteLine(del3("abc"));
#endregion

#region 匿名方法实例化委托
Console.WriteLine("-----以下是匿名方法演示--------");
//用匿名方法实例化委托
ProcessString p = delegate(string inputString) {
return inputString.ToUpper();
};
//通过委托调用匿名方法
Console.WriteLine(p("aaaa"));
#endregion

#region 委托多播演示

Console.WriteLine("-----以下是委托多播演示--------");
MyDel mydel1 = t.MyMethod;
MyDel mydel2 = t.MyMethod2;
MyDel mydel3 = TestDel.MyMethod3;
MyDel allMyDel = mydel1 + mydel2 + mydel3;
allMyDel();
Console.WriteLine("-----   allMyDel -= mydel3;多播演示--------");
allMyDel -= mydel3;
allMyDel();

#endregion

#region 委托作为参数演示

Console.WriteLine("-------以下是委托作为参数演示------");
MyDelStrUp paramMyDel3 = t.MyMethod;
TestDel.MyParamMethod("aaa", paramMyDel3);

#endregion

#region 委托作为返回值

Console.WriteLine("---以下是委托作为返回值演示------");
///returnMyDel指向t.MyReturnMethod()的返回值
MyDelStrUp returnMyDel = t.MyReturnMethod();
///returnMyDel指向t.MyMethod
//MyDel3 returnMyDel = t.MyMethod;

Console.WriteLine(returnMyDel("sssssssssssss"));

#endregion

#endregion

//MyReturnDelegateTest my = new MyReturnDelegateTest();
//my.MyTest();
MyParamDelegateTest myParam = new MyParamDelegateTest();
myParam.AddBooks();
myParam.MyTest();
Console.Read();
}
}
/// <summary>
///这是 类,有好多方法
/// </summary>
public class TestDel
{
#region 普通方法
/// <summary>
/// 静态方法
/// </summary>
public static void MyStaticMethod()
{
Console.WriteLine("My Static Method");
}
/// <summary>
/// 输出 mymethod
/// </summary>
public void MyMethod()
{
Console.WriteLine("MyMethod");
}
public void MyMethod2()
{
Console.WriteLine("My Method 22222222222");
}
/// <summary>
/// 静态函数
/// </summary>
public static void MyMethod3()
{
Console.WriteLine("My Method 3333333333333");
}
/// <summary>
/// 返回两个数的和
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
public void MyMethod(int num1, int num2)
{
Console.WriteLine(num1 + num2);
}
/// <summary>
/// 字符串变成大写
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public string MyMethod(string s)
{
return s.ToUpper();
}
#endregion

/// <summary>
/// 委托作为方法参数
/// </summary>
/// <param name="s"></param>
/// <param name="del3"></param>
public static void MyParamMethod(string s, MyDelStrUp del3)
{
Console.WriteLine(del3(s));
}

/// <summary>
/// 委托作为返回值
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public MyDelStrUp MyReturnMethod()
{
///返回符合委托规范的方法
return MyMethod;
}
}

// 委托作为参数

public class MyParamDelegateTest
{
BookDB bookDB = new BookDB();
public void AddBooks()
{
bookDB.AddBook(new Book() { BookID=1,BookName="C#",Price=123,IsPaperbook=true });
bookDB.AddBook(new Book() { BookID = 1, BookName = "C#", Price = 123, IsPaperbook = false });
bookDB.AddBook(new Book() { BookID = 2, BookName = "ASP.Net", Price = 12, IsPaperbook = true });
bookDB.AddBook(new Book() { BookID = 1, BookName = "ADO", Price = 23, IsPaperbook = false });
}
/// <summary>
/// 用来实例化委托
/// </summary>
/// <param name="b"></param>
public void TestProcessBook(Book b)
{
if (b.IsPaperbook)
{
Console.WriteLine(b.BookName);
}
}
double total = 0;
public void TotalPrice(Book b)
{
total += b.Price;
}
public void MyTest()
{

//ProcessBook p=TestProcessBook;
//ProcessBook p1=TotalPrice;
//ProcessBook p2=p+p1;
//把方法名做为参数进行传递
bookDB.PrintBook(TestProcessBook);
bookDB.PrintBook(TotalPrice);
Console.WriteLine(total);
}

}
/// <summary>
/// 委托
/// </summary>
/// <param name="b"></param>
public delegate void ProcessBook(Book b);

public class BookDB
{
public List<Book> books = new List<Book>();
public void AddBook(Book b)
{
books.Add(b);
}
/// <summary>
/// 委托做参数
/// </summary>
/// <param name="process">委托</param>
public void PrintBook(ProcessBook process)
{
foreach (var book in books)
{
//这里进行委托传进来的函数 操作book
process(book);
}
}
}

public class Book
{
public int BookID { get; set; }
public string BookName { get; set; }
public double Price { get; set; }
public bool IsPaperbook { get; set; }
}

//  委托作为返回值

public delegate int MyReturnDelegate(int num1, int num2);

public class MyReturnDelegateTest
{
public void MyTest()
{
MyCalcuate myCalcuate = new MyCalcuate();
do
{
Console.WriteLine("请输入符号进行以计算( + - * /)");
string oper = Console.ReadLine();
Console.WriteLine("请输入操作数1");
string num1 = Console.ReadLine();

Console.WriteLine("请输入操作数2");
string num2 = Console.ReadLine();

MyReturnDelegate myReturn = myCalcuate.Calcuate(oper);
int result = myReturn(int.Parse(num1), int.Parse(num2));
Console.WriteLine(
string.Format("{0}{1}{2}={3}", num1,oper,num2, result));

Console.WriteLine("您还要继续吗?Y/N");
//string continueFlag = Console.ReadLine();
//if (continueFlag.ToUpper() == "N") break;

} while (Console.ReadLine().ToUpper()!="N");
}
}

public class MyCalcuate
{
public MyReturnDelegate Calcuate(string oper)
{
MyReturnDelegate myReturn = null;
switch (oper)
{
case "+":
myReturn = delegate(int num1, int num2) { return num1 + num2; };
break;
case "-":
myReturn = delegate(int num1, int num2) { return num1 - num2; };
break;
case "*":
myReturn = delegate(int num1, int num2) { return num1 * num2; };
break;
case "/":
myReturn = delegate(int num1, int num2) { return num1 / num2; };
break;
default:
break;
}
return myReturn;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: