如何使用接口实现多重继承,但两个接口具有相同的方法名称 [英] how to achieve multiple inheritance using interface but both interface have same method name

查看:124
本文介绍了如何使用接口实现多重继承,但两个接口具有相同的方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同方法名称的接口和一个继承这些接口的类但是当我尝试创建类的对象并调用那些接口方法时,我怎么来知道如果我调用这两个方法将首先执行哪个方法.. ??

解决方案

试试这个





  interface  I1 
{
void diap();
}
interface I2
{
void diap ();
}

class Imp:I1,I2
{

public void I1.diap()
{
throw new 异常( 方法或操作没有实现。);
}



void I2.diap()
{
throw new 异常( 未实现方法或操作。);
}

}







_______________________________________________________



现在使用如下



 Imp f =  Imp(); 
I1 i = f; // I1接口I1用于选择I1行为使用I2作为第二个接口
i.diap ();







好​​运!!


这个问题的答案实际上取决于你如何在课堂上实现你的接口。换句话说,您是否明确地和/或隐式地实现了接口。让我们考虑一个简单的例子:

  public   interface  IFoo 
{
void SetName( string name);
int 年龄{获取; set ; }
}

public interface IBar
{
void SetName( string name);
DateTime DOB { get ; set ; }
}

public class FooBar:IFoo,IBar
{
public void SetName( string name)
{
Console.WriteLine( 名称是 + name);
}

public int 年龄
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

void IBar.SetName( string name)
{
Console.WriteLine( IBar Name is +名字);
}

DateTime IBar.DOB
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}

现在,让我们这样称呼:

 IFoo foo =  new  FooBar(); 
foo.SetName( Foo);
IBar bar = new FooBar();
bar.SetName( Bar);

这会产生输出

名称是Foo 
IBar名称是Foo

现在,如果我们在实例化中不使用接口,则调用显式实现。 pre lang =c#> FooBar fooBar = new FooBar();
fooBar.SetName( FooBar);

这会产生

名称是FooBar 

好的,您可能会想到声明中接口的顺序决定哪一个是显式的,哪一个是隐式声明。这是不正确的。让我们改变我们的类

公共类FooBar:IFoo,IBar 
{
public void SetName(string name)
{
Console.WriteLine (姓名+姓名);
}

int IFoo.Age
{
get
{
throw new NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}

void IFoo.SetName(字符串名称)
{
Console.WriteLine(IFoo Name is+ name);
}

public DateTime DOB
{
get
{
throw new NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
}

现在,调用与上面相同的代码,我们看到输出已经改变如下:

 IFoo名称是Foo 
名称是Bar
名称是FooBar

如您所见,IBar现在是显式实现。


i have two interface with same method name and one class inheriting those interface but when i trying to create object of class and call those interface method, how i come come to know which method will execute first if i call both method..??

解决方案

Try this


interface I1
   {
       void diap();
   }
   interface I2
   {
       void diap();
   }

class Imp:I1,I2
{

public void I1.diap()
        {
            throw new Exception("The method or operation is not implemented.");
        }           

       
       
        void I2.diap()
        {
            throw new Exception("The method or operation is not implemented.");
        }

}




_______________________________________________________

Now use as follows

Imp f = new Imp();
            I1 i = f;  //I1 interface I1 used to select I1 behaviour use I2 for second interface
            i.diap();




best of luck !!


The answer to this question really depends on how you have implemented your interfaces in the class. In other words, have you explicitly and/or implicitly implemented the interfaces. Let''s consider a simple example:

public interface IFoo
{
  void SetName(string name);
  int Age { get; set; }
}

public interface IBar
{
  void SetName(string name);
  DateTime DOB { get; set; }
}

public class FooBar : IFoo, IBar
{
  public void SetName(string name)
  {
    Console.WriteLine("Name is " + name);
  }

  public int Age
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }

  void IBar.SetName(string name)
  {
    Console.WriteLine("IBar Name is " + name);
  }

  DateTime IBar.DOB
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }
}

Now, let''s call this like this:

IFoo foo = new FooBar();
foo.SetName("Foo");
IBar bar = new FooBar();
bar.SetName("Bar");

This would produce the output

Name is Foo
IBar Name is Foo

Now, if we don''t use the interface to in the instantiation, the explicit implementation is called.

FooBar fooBar = new FooBar();
fooBar.SetName("FooBar");

This produces

Name is FooBar

Okay, so you may be tempted to think that the order of the interfaces in the declaration determines which one is the explicit and which one is the implicit declaration. This is not correct. Let''s change our class a bit

public class FooBar : IFoo, IBar
{
  public void SetName(string name)
  {
    Console.WriteLine("Name is " + name);
  }

  int IFoo.Age
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }

  void IFoo.SetName(string name)
  {
    Console.WriteLine("IFoo Name is " + name);
  }

  public DateTime DOB
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }
}

Now, calling the same code as we did above, we see that the output has changed like this:

IFoo Name is Foo
Name is Bar
Name is FooBar

As you can see, IBar is now the explicit implementation.


这篇关于如何使用接口实现多重继承,但两个接口具有相同的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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