C#函数调用的一个问题

云中客 2011-10-26 11:21:49
函数定义:
public static void ThoroughCollisionDetection(List<BaseLabel> labels)
{

}

函数调用:
layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;

问题:
为什么这里的LabelCollisionDetection.ThoroughCollisionDetection;函数调用没有给参数也能执行
...全文
134 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
迈克尔1998 2011-10-26
  • 打赏
  • 举报
回复
学习~
gxmark 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 sxycgxj 的回复:]
函数定义:
public static void ThoroughCollisionDetection(List<BaseLabel> labels)
{

}

函数调用:
layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;

问题:
为什么这里的LabelCollisi……
[/Quote]

这里应该不是函数调用,实际上是实例化一个委托对象,只是传递的参数是一个方法(函数)名
SQL777 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sxycgxj 的回复:]
C# code

using System.Collections.Generic;

namespace SharpMap.Rendering
{
/// <summary>
/// Class defining delegate for label collision detection and static predefined methods
///……
[/Quote]
就是一个方法赋值给一个委托..
风骑士之怒 2011-10-26
  • 打赏
  • 举报
回复
看错了,委托


class LayCityLabel
{
public delegate void A(List<int> i);
public A LabelFilter
{
get;
set;
}
}

class Program
{
public static void ThoroughCollisionDetection(List<int> labels)
{
foreach (int item in labels)
{
Console.WriteLine(item);
}
}
static void Main(string[] args)
{
LayCityLabel layCityLabel = new LayCityLabel();
layCityLabel.LabelFilter = ThoroughCollisionDetection;
layCityLabel.LabelFilter(new List<int>() { 1, 2, 3, 4 });
Console.Read();
}
}
云中客 2011-10-26
  • 打赏
  • 举报
回复

using System.Collections.Generic;

namespace SharpMap.Rendering
{
/// <summary>
/// Class defining delegate for label collision detection and static predefined methods
/// </summary>
public class LabelCollisionDetection
{
#region Delegates

/// <summary>
/// Delegate method for filtering labels. Useful for performing custom collision detection on labels
/// </summary>
/// <param name="labels"></param>
/// <returns></returns>
public delegate void LabelFilterMethod(List<BaseLabel> labels);

#endregion

#region Label filter methods

/// <summary>
/// Simple and fast label collision detection.
/// </summary>
/// <param name="labels"></param>
public static void SimpleCollisionDetection(List<BaseLabel> labels)
{
labels.Sort(); // sort labels by intersectiontests of labelbox
//remove labels that intersect other labels
for (int i = labels.Count - 1; i > 0; i--)
if (labels[i].CompareTo(labels[i - 1]) == 0)
{
if (labels[i].Priority == labels[i - 1].Priority) continue;

if (labels[i].Priority > labels[i - 1].Priority)
labels.RemoveAt(i - 1);
else
labels.RemoveAt(i);
}
}

/// <summary>
/// Thorough label collision detection.
/// </summary>
/// <param name="labels"></param>
public static void ThoroughCollisionDetection(List<BaseLabel> labels)
{
labels.Sort(); // sort labels by intersectiontests of labelbox
//remove labels that intersect other labels
for (int i = labels.Count - 1; i > 0; i--)
{
if (!labels[i].Show) continue;
for (int j = i - 1; j >= 0; j--)
{
if (!labels[j].Show) continue;
if (labels[i].CompareTo(labels[j]) == 0)
if (labels[i].Priority >= labels[j].Priority)
{
labels[j].Show = false;
//labels.RemoveAt(j);
//i--;
}
else
{
labels[i].Show = false;
//labels.RemoveAt(i);
//i--;
break;
}
}
}
}

#endregion
}
}
PaulyJiang 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sxycgxj 的回复:]
引用 1 楼 ssp2009 的回复:
还有上下文吗

有的,就是代码太长,没贴出来
[/Quote]
在 layCityLabel 这个对象的Class 中或者在其父类中
风骑士之怒 2011-10-26
  • 打赏
  • 举报
回复
你查看下layCityLabel类中,关于LabelFilter定义就好了
云中客 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wknight_it 的回复:]
public event Action<List<BaseLabel>> LabelFilter;

有个事件
[/Quote]

函数定义部分在一个类中
你所说的这个定义是在那个类中还是在当前代码中?
云中客 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ssp2009 的回复:]
还有上下文吗
[/Quote]
有的,就是代码太长,没贴出来
风骑士之怒 2011-10-26
  • 打赏
  • 举报
回复
public event Action<List<BaseLabel>> LabelFilter;

有个事件
快溜 2011-10-26
  • 打赏
  • 举报
回复
还有上下文吗

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧