惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
V
Visual Studio Blog
爱范儿
爱范儿
H
Help Net Security
J
Java Code Geeks
I
InfoQ
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
S
SegmentFault 最新的问题
S
Securelist
L
LINUX DO - 热门话题
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
博客园_首页
B
Blog
F
Fortinet All Blogs
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Schneier on Security
Project Zero
Project Zero
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic

博客园 - symjie

每天学一点AS3.0(五)---声音的控制(5) 每天学一点AS3.0(四)---声音的控制(4) 每天学一点AS3.0(三)---声音的控制(3) 每天学一点AS3.0(二)---声音的控制(2) 每天学一点AS3.0(一)---声音的控制 Jquery json的超强组合 - symjie - 博客园 javascript分页程序 - symjie - 博客园 初试jquery制作一个简单的loading sql 分页 Ajax.net和GridView交互 Ajax.net实现loading登陆的效果 - symjie - 博客园 Ajax.net+模态窗口的登陆简单例子 GridView自定义分页(vb) - symjie - 博客园 用下拉列表控制gridview的分页 DataView数据组件 (转) c#冒泡程序 Atlas学习笔记 getElementByTagsName和相关函数的学习 基于Ajax.net的验证
delegate——委派 - symjie - 博客园
symjie · 2007-09-10 · via 博客园 - symjie

 1using System;
 2namespace uu
 3{
 4 public class Delegates
 5 {
 6  public static void Main()
 7  {
 8   long length;
 9   long width;
10   long height;
11   Console.Write("请输入长度:");
12   length=long.Parse(Console.ReadLine());
13   Console.Write("请输入宽度");
14   width=long.Parse(Console.ReadLine());
15   Console.Write("请输入高度");
16   height=long.Parse(Console.ReadLine());
17   GGResult myGetResult=new GGResult(height,length,width);
18   Measure myMeasure=new Measure();
19   GGResult.MyDelegate myGRdelegate;
20   if(length==width)
21   {
22    myGRdelegate=new GGResult.MyDelegate(myMeasure.MeasureSquare);//提示要参数
23   }

24   else
25   {
26    myGRdelegate=new GGResult.MyDelegate(myMeasure.MeasureRectangle);//提示要参数
27   }

28   myGetResult.Calc(myGRdelegate);
29   Console.ReadLine();
30 }
 
31}

32//GGResult类
33 public class GGResult
34 {
35  long height;
36  long width;
37  long length;
38  public GGResult(long height,long width,long length)
39  {
40   this.height=height;
41   this.width=width;
42   this.length=length;
43  }

44  public delegate double MyDelegate(long length,long width);//声明委托
45  public void Calc(MyDelegate pmydelegate)
46  {
47   double volume;
48   volume=pmydelegate(length,width)*height;
49   Console.WriteLine("高度为{0}则体积为{1}",height,volume);
50  }

51 }

52 //Measure类
53 public class Measure
54 {
55  public double MeasureSquare(long length,long width)
56  {
57   double squareLength;
58   double squareSurface;
59   squareLength=4*length;
60   squareSurface=Math.Pow(length,2);
61   Console.WriteLine("长为{0}宽为{1}的正方的周长为{2},面积为{3}",length,width,squareLength,squareSurface);
62   return squareSurface;
63  }

64  public double MeasureRectangle(long length,long width)
65  {
66   double RectangleLength;
67   double RectangleSurface;
68   RectangleLength=2*(length+width);
69   RectangleSurface=length*width;
70   Console.WriteLine("长为{0}宽为{1}的长方形的周长为{2},面结为{3}:",length,width,RectangleLength,RectangleSurface);
71   return RectangleSurface;
72  }

73 }

74}

委派相当于一个容器,而这个容器使用来存放函数的或者说是用来封装函数的,而这个容器又可以看成一个函数,这个函数没有
   实现方法,只需要返回类型和参数与姚封装的函数一致就可以了,那么就可以封装这个函数了。
   例如上面的例子:
   1,声明了一个double型的委派myDelegate,参数为(long length,long width);那么此委派只能封装就有返回值且是double,而且
   参数为long的函数。
   2,在Measure类中,有两个方法,MeasureSquare和MeasureRectangle,这两个函数都具有反回值是double型的,参数也是long
   型的。所以这两个函数都可以分装到委派里面。
   3,上面说到,委派相当于一个函数,既然是函数,那么也就可以有参数和返回值(当然了要和所封装的函数一致)。
      1,在GGResult类中有一个方法Calc;它的参数为委派类型的参数,实际上是调用了委派所封装的方法,根据条件来初始化
      myDelegate所封装的方法,然后把这个方法通过参数的形式传送到Cale中,然后再Calc中调用这个方法。
   大概一看此程序没有什么可看的,其实不然,简单的看一下似乎没有问题,但仔细一想问题就出来了
   myDelegate=new GGResult.MyDelegate(myMeasure.MeasureSquare);
   myDelegate=new GGResult.MyDelegate(myMeasure.MeasureRectangle);
   这两句是初始化了委派即说明了委派来封装那个函数,并不是执行语句。那么什么时候开始执行这个委派呢?
   在 myGetResult.Calc(myGRdelegate);这句中,因为在Calc中有这么一句
   volume=pmydelegate(length,width)*height;
   所以在执行pmydelegate时就开始执行了上面的其中一个委派中的方法。
   那么参数从何而来?答案就是:在初始化GGResult时就把参数初始化到GGResult的构造函数中了,然后调用pmydelegate时
   就直接调用了。
  
     
   注意:在类中声明函数时,如果函数没有static关键字时,那么可以通过类的实例来引用,如果有static关键字时,只能用
   类名直接引用。
   类中类的初始化:
   在GGResult中声明了一个委派类MyDelegate那么在初始化时必须加上GGResult
   既GGResult.MyDelegate   实例名字=new GGResult.MyDelegate();