2011-05-11 40 views
5

我不知道如何编写接口工作在C#中,例如这里描述: codeguru explanation什么是在C#中的接口铸造?

interface IIntelligence 
    { 
     bool intelligent_behavior(); 
    } 

    class Human: IIntelligence 
    { 
     public Human() 
     { 
      //............. 
     } 

/// Interface method definition in the class that implements it 
     public bool intelligent_behavior() 
     { 
     Console.WriteLine("........"); 
     return true 
     } 
    } 

不过,我困惑接口铸造以下过程:

Human human = new Human(); 
// The human object is casted to the interface type 
IIntelligence humanIQ = (IIntelligence)human; 
humanIQ.intelligent_behavior(); 

什么(人类在这种情况下)执行一个接口,然后将其实例人类回到接口?问题不在于它是如何工作的,但为什么它完成。

回答

0

它可以访问显式的接口实现。

有时你想隐藏类实现接口的事实。这是通过明确实现接口来完成的。

public class MyClass : IInterface 
{ 
    string IInterface.TheMethod(){} 
} 
2

有时你可能不知道对象是什么,但你知道它实现了某个接口。

+0

可能会发生,我想你会做'baseObject is IInterface'来检查。好奇的是,方法参数没有被输入到'IInterface'。 – Jodrell 2011-05-11 09:10:41

0

出于同样的原因,你会派生一个派生类回到它的基类。

0

一个简短而流行的例子。我们可以实现这样的代码: interface IIntelligence { string Talk(); }

class Cat: ICreature 
    { 
     public string Talk() 
     { 
     Console.WriteLine("Meow!"); 
     return true 
     } 
    } 

    class Dog: ICreature 
    { 
     public string Talk() 
     { 
     Console.WriteLine("Arf!"); 
     return true 
     } 
    } 

    class Human: ICreature 
    { 
     public string Talk() 
     { 
     Console.WriteLine("Hello!"); 
     return true 
     } 
    } 

,然后我们可以使用下面的代码:

ICreature() creatures = new ICreature(){new Human(), new Dog(), new Cat()}; 
foreach(IIntelligence creature in creatures){ 
    Console.WriteLine(creature.Talk()); 
} 

有关详细信息,请参阅“多态性是面向对象程序设计”在谷歌。

0

考虑这种情况。我已经为您的示例添加了一个方法。

interface IIntelligence 
{ 
    bool intelligent_behavior(); 
} 

class Human: IIntelligence 
{ 
    public Human() { } 

    /// Interface method definition in the class that implements it 
    public bool IIntelligence.intelligent_behavior() 
    { 
     Console.WriteLine("........"); 
     return true; 
    }  

    //Some other method definition 
    public bool intelligent_behaviour() 
    { 
     return false; 
    } 
} 

您将转换为IIntelligence以获得您想要的方法实现。

4

.net提供了两种类型的接口实现隐式实现和显式实现。

当您使用隐式的实现,它将成为类型接口本身的一部分。例如,如果你有一个IPerson接口这样的:

public interface IPerson 
{ 
string Name{get;} 
} 

你实现它,如下所示:

public class Person:IPerson 
{ 
public string Name{get; ....} 
} 

你可以这样访问它(隐含的):

aPerson.Name; 

但如果实现像这样(明确):

public class Person:IPerson 
{ 
string IPerson.Name{get; ....} // notice that there's no need to include access modifier. 
} 

那么就只能使用IPerson接口访问:

((IPerson)aPerson).Name; 

UPDATE:

实际上,显式接口实现使我们能够实现不同与具有相同名称的成员接口(如this Tutorial所示)

0

我发现了在开发时有用的接口g主要应用程序的插件。我创建了三个项目。第一个项目'connectorInterface'只包含一个类接口的类定义。接口代码:

public interface IConnectorDataReader 
{ 
    int ColumnCount 
    { 
    get; 
    } 

    bool readNextRecord(); 

    string this[int i] 
    { 
    get; 
    } 

    void reset(); 
} 

第二个项目“dataSource1”(插件主要应用)实现IConnectorDataReader接口,还实现了接口有一些额外的私有方法的类。第三个项目的主要应用程序的使用插件“dataSource1”时使用此代码来读取数据从插件“dataSource1”:

Assembly assembly = Assembly.LoadFile(path); // path to dll 
    Type type = assembly.GetType("dataSource1.DataReader"); 
    object myInstance = Activator.CreateInstance(type); 

    MethodInfo method = type.GetMethod("getConnectorDataReader"); 
    object data = method.Invoke(myInstance, null); 

    IConnectorDataReader reader =(IConnectorDataReader)data; 

    // method usage 
    while (reader.readNextRecord() == true) .... 

在我的情况铸件读取插件数据非常有用。我不在乎插件如何实现,只要它实现了通用接口。我所关心和使用的是阅读数据的常用方法。我想接口是有用的,也回到接口。