数组并不满足OO的替换……(同样的东西还有struct等)
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Son[] sons = new Son[5];
Father[] fathers = sons;
TestMethod(fathers);
}
private void TestMethod(Father[] ff)
{
ff[0] = new Daughter();//Here throw a exception.
}
private class Father { }
private class Daughter : Father { }
private class Son : Father { }
本文转自BloodyAngel博客园博客,原文链接:http://www.cnblogs.com/zgynhqf/archive/2009/11/21/1607711.html,如需转载请自行联系原作者
本文通过一个具体的C#代码示例探讨了数组在面向对象编程中的一些限制,特别是在使用继承时,父类数组不能直接存储子类对象的问题,并引发了一个异常。
1079

被折叠的 条评论
为什么被折叠?



