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 ++
但是会出现相同的编译器消息。