导航

委派/委托

Posted on 2010-06-07 16:23  杨彬Allen  阅读(215)  评论(0)    收藏  举报
delegate
using System;

public delegate double MathFunction();

public class Addition
{
double x, y;
public Addition(double x, double y)
{
this.x = x;
this.y = y;
}

public double Add()
{
return x + y;
}
}

public class Multiplication
{
double x, y;
public Multiplication(double x, double y)
{
this.x = x;
this.y = y;
}

public double Multiply()
{
return x * y;
}
}

public class Division
{
double x, y;
public Division(double x, double y)
{
this.x = x;
this.y = y;
}

public double Divide()
{
return x / y;
}
}

public partial class Default9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
double x = 3.0;
double y = 4.0;
double z, z1, z2;
Addition a
= new Addition(x, y);
Multiplication m
= new Multiplication(x, y);
Division d
= new Division(x, y);
z
= Apply(x, y, a.Add);
z1
= Apply(x, y, m.Multiply);
z2
= Apply(x, y, d.Divide);
Response.Write(z.ToString()
+ "<br/>" + z1.ToString() + "<br/>" + z2.ToString());
}

protected double Apply(double x, double y, MathFunction mf)
{
double z = mf();
return z;
}
}