【发布时间】:2009-11-18 07:55:25
【问题描述】:
假设我有代码段
namespace Test
{
public delegate void StaticticsDelegate(object sender,EventArgs e);
class DynamicDelegates
{
static void Main()
{
StaticticsDelegate del = BowlerStatistics;
BallThrownEventArgs be = new BallThrownEventArgs(90, 145);
del(DynamicDelegates.Main,be);
del = BatsmanStatistics;
RunsScoredEventArgs re = new RunsScoredEventArgs("Skeet", 55);
del(DynamicDelegates.Main, re);
Console.ReadLine();
}
static void BowlerStatistics(object sender, BallThrownEventArgs e)
{
Console.WriteLine
("{0} sender is ; Bowler Statistics :speed {1} angle {2}",
sender, e.Speed, e.Angle);
}
static void BatsmanStatistics(object sender, RunsScoredEventArgs e)
{
Console.WriteLine
("{0} sender is ; Batsman Statistics :name {1} runs {2}",
sender, e.PlayerName, e.Runs);
}
}
class BallThrownEventArgs:EventArgs
{
int angle;
int speed;
public BallThrownEventArgs(int angle, int speed)
{
this.angle = angle;
this.speed = speed;
}
public int Angle
{
get { return angle; }
}
public int Speed
{
get { return speed; }
}
}
class RunsScoredEventArgs : EventArgs
{
string playerName;
int runs;
public RunsScoredEventArgs(string playerName, int runs)
{
this.playerName = playerName;
this.runs = runs;
}
public string PlayerName
{
get { return playerName; }
}
public int Runs
{
get { return runs; }
}
}
}
1) 既然“BallThrownEventArgs”和“RunsScoredEventsArgs”是从“EventArgs”派生的,为什么 是否生成了错误“匹配没有重载...” 当我执行“Main()”时。
2)你所说的“ContraVariance”和“Covariance”是什么意思(请举个例子来理解)?
3) 术语“逆变”和“协变”仅与委托有关?
【问题讨论】:
-
协/逆变相关问题:stackoverflow.com/questions/1120688/…>