接口模棱两可 [英] Interface ambiguous

查看:72
本文介绍了接口模棱两可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class CustomCllection
    {
        C c1 = new XYZ();
        A a1 = new XYZ();
        B b1 = new XYZ();
        XYZ x = new XYZ();
        public void Check()
        {
            a1.i = 30;
            a1.Test1();
            a1.Test2();
            //-----------
            b1.k = 30;
            b1.Test2();
            b1.Test3();
            //----------------
            c1.i = 40;
            c1.k = 50;
            c1.m = 60;
            c1.Test1();
            c1.Test2(); // [Edit]Error is here
            c1.Test3();
            c1.Test4();
        }
    }
    interface A
    {
        void Test1();
        void Test2();
        int i { get;set;}
    }
    interface B
    {
        void Test2();
        void Test3();
        int k { get;set;}
    }
    interface C : B, A
    {
       new void Test3();
        void Test4();
        int m { get;set;}
    }
    class XYZ : C
    {
        public void Test1()
        {
            MessageBox.Show("Hello I am Test1");
        }
        void A.Test2()
        {
            MessageBox.Show("Hello I am Test2(A)");
        }
        void B.Test2()
        {
            MessageBox.Show("Hello I am Test2(B)");
        }
        public void Test3()
        {
            MessageBox.Show("Hello I am Test3");
        }
        public void Test4()
        {
            MessageBox.Show("Hello I am Test4");
        }
        int nn = 10;
        public int i
        {
            set { value = nn; }
            get { return nn; }
        }
        int nm = 10;
        public int m
        {
            set { value = nm; }
            get { return nm; }
        }
        int km = 10;
        public int k
        {
            set { value = km; }
            get { return km; }
        }

    }




给我们错误:
错误1以下方法或属性之间的调用是模棱两可的. A.Test2()和B.Test2()
任何人都可以解释为什么会发生这种模棱两可的情况.

由DaveyM69添加.




Give Us Error:
Error 1 The call is ambiguous between the following methods or properties. A.Test2() and B.Test2()
Can Any One Explain Why this ambiguous Occur.

Added by DaveyM69

推荐答案

因为AB都具有带有相同参数(无)的Test2方法.
由于C是从两者衍生而来的,因此不知道使用哪个.

您的其他测试实例将转换为AB,因此它们将使用各自的方法.
Beacuase both A and B have a Test2 method with the same parameters (none).
As C is derived from both it doesn''t know which to use.

Your other test instances are down cast to A or B so they use their respective methods.


完整的错误消息是:

The call is ambiguous between the following methods or properties: ''B.Test2()'' and ''A.Test2()''

哪个应该可以帮助您确定问题出在哪里
The complete error message is :

The call is ambiguous between the following methods or properties: ''B.Test2()'' and ''A.Test2()''

Which should help you to identify what the problem is


这篇关于接口模棱两可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆