是否可以在一个类中具有多个接口?如果可以的话怎么办? [英] Is it possible to have multiple interface in a single class ? if so how ?

查看:123
本文介绍了是否可以在一个类中具有多个接口?如果可以的话怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

在我们的应用程序中(在c#或vb.net中),
一个类中可以有多个接口吗?
如果可以的话怎么办?如果是的话,

在以下情况下(在c#或vb.net中),例如,
单班
{
接口1;
接口2;
接口3;
}

在此,程序如何识别以及运行哪个界面
第一,第二和第三...

在此先感谢

S.Naveen ...

Hi all

In our application (either in c# or vb.net),
Is it possible to have multiple interface in a single class ?
if so how ? and if so ,

in the following scenario (either in c# or vb.net) for eg.,
Single Class
{
interface 1;
interface 2;
interface 3;
}

In this ,how the program identifies and which interface runs
first ,second and third...

Thanks in advance

S.Naveen...

推荐答案

实现多个接口没有问题,但是我想您正在尝试区分多个接口之间的冲突?

为此,您需要显式实现接口.在C#中,看起来类似于以下内容:

There is no problem implementing multiple interfaces, but I imagine what you''re trying to do is differentiate between collisions in multiple interfaces?

In order to do this you need to explicitly implement the interface. In c# that would look something like the following:

public class Class1 : IInterface1, IInterface2
{
    string IInterface1.foo
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    string IInterface2.foo
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}



两者之间没有顺序.您不能调用其中一个,而另一个自动执行.您需要从另一个方法的实现中显式调用一个方法.

如果您想有选择地调用任一方法,则可以执行以下操作:



There is no order between the two; you can''t call one and have the other automagically execute. You would need to explicitly call one method from the implementation of the other.

If you want to call either selectively, you would do something like this:

Class1 theClass = new Class1();

IInterface1 i1 = theClass as IInterface1;
IInterface2 i2 = theClass as IInterface2;

string bar1 = i1.foo; // executes the IInterface1 version
string bar2 = i2.foo; // executes the IInterface2 version



干杯.



Cheers.


有很多方法可以使程序按特定顺序执行代码.根据问题描述得不好的最好方法是让每个接口的foo方法以所需的顺序调用另一个接口的foo方法.我不知道一种指定顺序的方法,只是勉强使用它.
There are many ways to cause a program to execute code in a specific order. The best way for you to do it based on your poorly described question is have each interface''s foo method call another interface''s foo method in the order you want. I don''t know of a way to specify the order though, short of just hard-wiring it.


谢谢James先生

但是我还是有点困惑,

代码如何知道首先运行哪个接口(第一个或第二个)?

是否需要我们按顺序给以提示,否则我们可以通过一些
具体说明,使其遵循界面的顺序...

在此先感谢

Naveen ...
Thank you Mr.James

But still im little confused like,

How the code knows to run which interface first ,either 1st or 2nd ?

is it needed that we should giv in order or we can pass some
specific instructions ,so that it follows the order of interface...

Thanks in advance

Naveen...


这篇关于是否可以在一个类中具有多个接口?如果可以的话怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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