我有一段以下代码,定义一个接口iInterface,cBase实现iInterface,cChild继承cBase,UML为css
预期是想要cBase.F()的执行逻辑,同时须要cChild的返回值,因此FF预期的输出html
1: namespace ConsoleApplication1
2: {
3: class Program
4: {
5: static void Main(string[] args)
6: {
7: iInterface inf = new cChild();
8: FF(inf);
9:
10: Console.ReadLine();
11: }
12: static public void FF(iInterface inf)
13: {
14: Console.WriteLine(inf.F());
15: }
16: }
17:
18: public interface iInterface
19: {
20: string F();
21: }
22: public class cBase : iInterface
23: {
24: public string F()
25: {
26: Console.WriteLine("base.F1");
27: return "this is base";
28: }
29: }
30: public class cChild : cBase
31: {
32: public string F()
33: {
34: base.F();
35: return "this is child";
36: }
37: }
38: }
可是实际的结果是只能call到cBase的F函数,输出”this is base”函数
缘由是咱们定义的inf类型为iInterface,同时赋值的对象类型为cChild,可是cChild是没有实现iInterface的(继承至实现了接口的class也是不行的)。this
实际运行时,inf向上找到了cBase有实现iInterface,因此调用了它的实现。spa
想达到预期的结果,能够将cChild也实现iInterface,以下:code
1: public class cChild : cBase, iInterface
2: {
3: public string F()
4: {
5: base.F();
6: return "this is child";
7: }
8: }
修改后的UML为htm
输出结果为:对象