使用接口变量 [英] Using Interface variables

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

问题描述

我仍然试图得到一个更好的理解接口。我知道它们是什么以及如何实现它们的类。

I'm still trying to get a better understanding of Interfaces. I know about what they are and how to implement them in classes.

我不明白的是,当你创建一个变量,它是你的接口类型之一:

What I don't understand is when you create a variable that is of one of your Interface types:

IMyInterface somevariable;

你为什么会这么做?我不明白怎么IMyInterface的可以像一个类中使用...例如调用方法,所以:

Why would you do this? I don't understand how IMyInterface can be used like a class...for example to call methods, so:

somevariable.CallSomeMethod();

为什么要使用一个变量IMyInterface的做到这一点?

Why would you use an IMyInterface variable to do this?

推荐答案

您不能创建接口的实例 - 要创建的东西,实现接口的实例

You are not creating an instance of the interface - you are creating an instance of something that implements the interface.

接口的一点是,它保证了什么都实现它会提供在其内声明的方法

The point of the interface is that it guarantees that what ever implements it will provide the methods declared within it.

所以,现在,使用例如,你可以有:

So now, using your example, you could have:

MyNiftyClass : IMyInterface
{
    public void CallSomeMethod()
    {
        //Do something nifty
    }
}

MyOddClass : IMyInterface
{
    public void CallSomeMethod()
    {
        //Do something odd
    }
}

现在您有:

IMyInterface nifty = new MyNiftyClass()
IMyInterface odd = new MyOddClass()

调用CallSomeMethod方法现在会做的东西要么漂亮或什么奇怪的,这时候你逝去的使用IMyInterface的作为类型变得格外有用。

Calling the CallSomeMethod method will now do either something nifty or something odd, and this becomes particulary useful when you are passing in using IMyInterface as the type.

public void ThisMethodShowsHowItWorks(IMyInterface someObject)
{
    someObject.CallSomeMethod();
}

现在,这取决于您是否调用上述方法用一记漂亮的或奇数类,你会得到不同的行为。

Now, depending on whether you call the above method with a nifty or an odd class, you get different behaviour.

public void AnotherClass()
{
    IMyInterface nifty = new MyNiftyClass()
    IMyInterface odd = new MyOddClass()

    // Pass in the nifty class to do something nifty
    this.ThisMethodShowsHowItWorks(nifty);

    // Pass in the odd class to do something odd
    this.ThisMethodShowsHowItWorks(odd);

}

修改

这个地址是什么,我认为你的预期的问题是 - ?为什么你会声明一个变量是一个接口类型

This addresses what I think your intended question is - Why would you declare a variable to be of an interface type?

这就是为什么要使用:

IMyInterface foo = new MyConcreteClass();

在preference为:

in preference to:

MyConcreteClass foo = new MyConcreteClass();

希望这是明确的,你为什么会用声明的方法签名时界面,但还有约局部范围变量的问题:

Hopefully it is clear why you would use the interface when declaring a method signature, but that leaves the question about locally scoped variables:

public void AMethod()
{
    // Why use this?
    IMyInterface foo = new MyConcreteClass();

    // Why not use this?
    MyConcreteClass bar = new MyConcreteClass();
}

通常没有技术上的原因,该接口preferred。我通常使用的接口,因为:

Usually there is no technical reason why the interface is preferred. I usually use the interface because:


  • 我所以需要多态性通常注入依赖

  • 使用界面明确指出我的意图接口的唯一成员使用

在一个地方,你会的技术上的所需要的接口,你正在使用的多态性,例如使用一个工厂或使用依赖注入(如我上面说的)创建变量。

The one place where you would technically need the interface is where you are utilising the polymorphism, such as creating your variable using a factory or (as I say above) using dependency injection.

中借鉴itowlson为例,用具体的声明,你可以这样做:

Borrowing an example from itowlson, using concrete declaration you could not do this:

public void AMethod(string input)
{               
    IMyInterface foo;

    if (input == "nifty")
    {
        foo = new MyNiftyClass();
    }
    else
    {
        foo = new MyOddClass();
    }
    foo.CallSomeMethod();
}

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

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