什么是专用接口? [英] What is a private interface?

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

问题描述

在接受记者采访前一阵子了.NET位置面试官问我你会使用一个专用接口?

我问他,他才意味着VS显式接口实现到他回答没有隐含的区别。

所以我想知道:


  1. 他的意思?

  2. 您会用哪一个专用接口?


解决方案

这是界面可以在另一个类中是私有的

 公共类MyClass的
{
    专用接口的IFoo
    {
    INT MyProp {搞定; }
    }    私有类Foo:IFoo的
    {
    公众诠释MyProp {搞定;组; }
    }    公共静态无效的主要(字串[] args)
    {
    IFoo的富=新的Foo();
    返回foo.MyProp;
    }
}

在实用性方面它只是从其他code隐藏,即使在同一组件,此说接口的存在。这样做的效用是不是在我看来,非常高的。

显式接口实现是一个不同的问题,有一些非常有用的情况(特别是与泛型和老的非通用接口的工作),但我不会把它称为专用接口,不会说这个词以这种方式常用。

使用两种技术一起,你可以这样做:

 公共类MyClass的
{
    专用接口的IFoo
    {
    INT MyProp {搞定; }
    }    公共类Foo:IFoo的
    {
    INT IFoo.MyProp {搞定;组; }
    }    公共静态无效的主要(字串[] args)
    {
    IFoo的富=新的Foo();
    返回foo.MyProp;
    }
}公共类HiddenFromMe
{
    公共静态无效的主要(字串[] args)
    {
    MyClass.Foo富=新MyClass.Foo();
    返回foo.MyProp; //编译失败
    }
}

这可以让你暴露嵌套类以某种方式同时允许父类调用它们的方法,外界不能。这是一个潜在有用的情况下,但不是我想经常使用。当然,它的被采访者是使用边界情况在接受采访时用之嫌,因为他们已经看到了它,虽然它是有趣

In an interview a while ago for a .NET position the interviewer asked me "what would you use a private interface for?".

I asked him did he mean the difference between implicit vs explicit interface implementation to which he answered no.

So I'm wondering:

  1. What he meant?
  2. What you would use a private interface for?

解决方案

An interface could be private within another class

public class MyClass
{
    private interface IFoo
    {
    	int MyProp { get; }
    }

    private class Foo : IFoo
    {
    	public int MyProp { get; set; }
    }

    public static void Main(string[] args)
    {
    	IFoo foo = new Foo();
    	return foo.MyProp;
    }
}

in terms of utility it simply hides from other code, even within the same assembly, that said interface exists. The utility of this is not terribly high in my opinion.

Explicit interface implementation is a different matter, has some very useful cases (especially when working with generics and older non generic interfaces) but I would not term it 'private interfaces' and would not say that the term is commonly used in that manner.

Using the two techniques together you can do:

public class MyClass
{
    private interface IFoo
    {
    	int MyProp { get; }
    }

    public class Foo : IFoo
    {
    	int IFoo.MyProp { get; set; }
    }

    public static void Main(string[] args)
    {
    	IFoo foo = new Foo();
    	return foo.MyProp;
    }
}

public class HiddenFromMe
{
    public static void Main(string[] args)
    {
    	MyClass.Foo foo = new MyClass.Foo();
    	return foo.MyProp; // fails to compile
    }
}

This allows you to expose the nested classes in some fashion while allowing the parent class to invoke methods on them that the outside world cannot. This is a potentially useful case but is not something I would wish to use very often. Certainly it's use in an interview smacks of being a boundary case the interviewer is using because they've seen it and though it was 'interesting'

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

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