假設我有代碼段C#:繼承@代表
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)術語「逆變」和「協方差」僅與代表有關?
約CO /逆變相關問題: –
Konamiman
2009-11-18 08:00:01