【发布时间】:2013-11-01 10:18:26
【问题描述】:
C# 类是否从基类继承自定义运算符?我正在尝试以下代码:
class Person
{
public int Age;
public static Person operator ++(Person p)
{
p.Age++;
return p;
}
}
class Agent : Person { }
static void Main(string[] args)
{
Person p = new Person { Age = 23 };
Console.WriteLine ( "Before increment : {0}" , p.Age );
p++;
Console.WriteLine("After increment : {0}", p.Age);
Agent agent = new Agent { Age = 25 };
Console.WriteLine("Before increment : {0}", agent.Age);
agent++;
Console.WriteLine("After increment : {0}", agent.Age);
}
编译器告诉我他不能显式地从 Person 转换为 Agent。
我试过了:
Agent agent = new Agent();
Person person = agent ++
但出现相同的编译器消息。
【问题讨论】:
-
错误发生在哪一行?
-
当我尝试增加“代理”变量时发生错误。
-
你问题的标题好像和内容没有任何关系。