C#(委託a)
5716 ワード
C#( a)
public delegate double TheOperator(double x, double y);
public class Operators
{
static public double Add(double x, double y)
{
return x + y;
}
static public double Sub(double x, double y)
{
return x - y;
}
// ,
}
public class DotNet_Calculator
{
public double Foo(TheOperator op, double x, double y)
{
return op(x, y);
}
}
public class TheMainClass
{
static public void Main()
{
TheOperator myAdd = new TheOperator(Operators.Add);
TheOperator mySub = new TheOperator(Operators.Sub);
DotNet_Calculator dc = new DotNet_Calculator();
double sum = dc.Foo(myAdd, 2, 4); //sum = 6
System.Console.WriteLine(sum);
double sub = dc.Foo(mySub, 3, 7); //sub = -4
System.Console.WriteLine(sub);
}
}
C#( b)
//
public class MrZhang
{
//
public static void BuyTicket()
{
Console.WriteLine("NND, , !");
}
}
//
class MrMing
{
// , “ ”
public delegate void BugTicketEventHandler();
public static void Main(string[] args)
{
// , MrZhang.BuyTicket“ ”
BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);
//
myDelegate();
Console.ReadKey();
}
}