委托:组合委托的例子

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

namespace  AspNetTest.Common
{
    
/// <summary>
    
/// Degalete_Math_Op 的摘要说明。
    
/// </summary>

    public class Define_Degalete_MathOp : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            MathOp mathOp 
= new MathOp(1,0);
            mathOp.opDelegate 
= new MathOp.OpDelegate(OperateFunc.Add);
            Response.Write(
"加:" + mathOp.Operate() + "<br>");
            mathOp.opDelegate 
= new MathOp.OpDelegate(OperateFunc.Subtract);
            Response.Write(
"减:" + mathOp.Operate() + "<br>");
            mathOp.opDelegate 
= new MathOp.OpDelegate(OperateFunc.Multiply);
            Response.Write(
"乘:" + mathOp.Operate() + "<br>");
            mathOp.opDelegate 
= new MathOp.OpDelegate(OperateFunc.Divide);
            Response.Write(
"除:" + mathOp.Operate() + "<br>");
            Response.Write(
"<hr>");
            
            
//改变组合委托的参数
            mathOp.opDelegate = null;
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Add);
            mathOp.op2 
= 1;
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Subtract);
            Response.Write(
"改变第二个组合委托的参数后,减操作的值为:" + mathOp.Operate() + "<br>");                        
            Response.Write(
"<hr>");
            
            mathOp.opDelegate 
= null;
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Add);
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Subtract);
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Multiply);
            mathOp.opDelegate 
+= new MathOp.OpDelegate(OperateFunc.Divide);
            
//以下输出的是 0,即最后一个Divide的return值
            Response.Write("组合委托的return值是:" + mathOp.Operate() + "<br>");
            Response.Write(
"<hr>");
            
            mathOp.opResponseDelegate 
= null;
            mathOp.opResponseDelegate 
+= new MathOp.OpResponseDelegate(OperateFunc.ResponseAdd);
            mathOp.opResponseDelegate 
+= new MathOp.OpResponseDelegate(OperateFunc.ResponseSubtract);
            mathOp.opResponseDelegate 
+= new MathOp.OpResponseDelegate(OperateFunc.ResponseMultiply);
            mathOp.opResponseDelegate 
+= new MathOp.OpResponseDelegate(OperateFunc.ResponseDivide);    
            Response.Write(
"组合委托的所有执行输出值是:<br>");    
            mathOp.ResponseOperate();    
        }

        
        
public class MathOp
        
{
            
public MathOp(int op1, int op2)
            
{
                
this.op1 = op1;
                
this.op2 = op2;
            }

            
public delegate int OpDelegate(int op1, int op2);
            
public delegate void OpResponseDelegate(int op1, int op2);
            
public OpDelegate opDelegate;
            
public OpResponseDelegate opResponseDelegate;
            
public int Operate()
            
{
                
if(opDelegate != null)
                
{
                    
int result = opDelegate(this.op1, this.op2);
                    
return result;
                }

                
else
                
{
                    
throw new NullReferenceException();
                }

            }

            
public void ResponseOperate()
            
{
                
if(opResponseDelegate != null)
                
{
                    opResponseDelegate(
this.op1, this.op2);
                }

                
else
                
{
                    
throw new NullReferenceException();
                }

            }

            
private int op1;
            
public int op2;
        }

        
        
public class OperateFunc
        
{
            
public static int Add(int op1, int op2)
            
{
                
return (int)op1+op2;
            }

            
public static int Subtract(int op1, int op2)
            
{
                
return (int)op1-op2;
            }

            
public static int Multiply(int op1, int op2)
            
{
                
return (int)op1*op2;
            }

            
//表示整除
            public static int Divide(int op1, int op2)
            
{
                
return op2==0 ? 0 : (int)op1/op2;
            }

            
            
            
public static void ResponseAdd(int op1, int op2)
            
{
                
int result = (int)op1+op2;
                HttpContext.Current.Response.Write(
"加:" + result + "<br>");
            }

            
public static void ResponseSubtract(int op1, int op2)
            
{
                
int result = (int)op1-op2;
                HttpContext.Current.Response.Write(
"减:" + result + "<br>");                
            }

            
public static void ResponseMultiply(int op1, int op2)
            
{
                
int result = (int)op1*op2;
                HttpContext.Current.Response.Write(
"乘:" + result + "<br>");
            }

            
//表示整除
            public static void ResponseDivide(int op1, int op2)
            
{
                
int result = (op2==0 ? 0 : (int)op1/op2);
                HttpContext.Current.Response.Write(
"除:" + result + "<br>");
            }

        }

        
        
        
        
        
        
        
        
        
Web 窗体设计器生成的代码
    }

}

你可能感兴趣的:(例子)