限制类以仅访问接口中的某些特定方法 [英] Restrict classes to access only some particular method in an Interface

查看:94
本文介绍了限制类以仅访问接口中的某些特定方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我从面试官那里得到的问题对我来说很复杂请给我更好的解决方案。



接口I1 
{
method1()
method2()
}

A类:I1
{
\\只能访问method1而不是method2
}

B类:I1
{
\\\ \\只能访问method2而不是method1
}





这是可能的吗?

解决方案

你可以在某种程度上做到这一点。如果使用接口实现将对实现类的引用作为对某些代码的接口的引用传递,则整个接口当然是可访问的。但是,如果将引用传递给类(在大多数情况下不推荐),则可以隐式实现一个方法,另一个显式实现:



 答:I1 {
public void Method1(){ / * ... * / } // 隐式,通过类和接口引用可见
void I1.Method2(){ / * ... * / } // 显式,仅通过接口参考可见,
/ / 不通过类引用
}

// ...

I1 implementation = new A ( / * ... * / );
implementation.Method1(); // 确定
implementation.Method2(); // 确定

implementationClassInstance = new A( / * ... * / ); // 相同的对象,
// 对它的不同类型的引用
implementClassInstance.Method1(); // 确定
// implementClassInstance.Method2(); //将无法编译



有想法吗?







参见:

http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/vstudio/ms173157.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/vstudio/44a9ty12.aspx [ ^ ]。



-SA


这是不可能的,该任务应该为不同的类单独创建接口。



接口I1 
{
method1()
}

接口I2
{
method2()
}
A类:I1
{
\ \只能访问method1而不是方法2
}

B类:I2
{
\\只能访问method2而不是method1
}


Dear All,

I got the question from Interviewer which is complicated for me pls give me the better solution.

Interface I1
{
    method1()
    method2()
}

Class A : I1
{
   \\ can Access only method1 not method2
}

Class B : I1
{
   \\ can Access only method2 not method1
}



Is That possible???

解决方案

You can do it, to certain extent. If you pass a reference to implementing class as the reference to the interface to some code using the interface implementation, the whole interface is of course accessible. However, if you pass the reference to a class (not recommended in most cases), you can implement one method implicitly, and another one explicitly:

class A: I1 {
   public void Method1() { /* ... */ } // implicit, visible through both class and interface references
   void I1.Method2() { /* ... */ } // explicit, visible through interface reference only,
                                   // not through the class reference
}

//...

I1 implementation = new A(/* ... */);
implementation.Method1(); // OK 
implementation.Method2(); // OK

A implementingClassInstance = new A(/* ... */); // same object,
                                                // different kind of reference to it
implementingClassInstance.Method1(); // OK 
//implementingClassInstance.Method2(); // won't compile


Got the idea?

[EDIT]

See also:
http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx[^],
http://msdn.microsoft.com/en-us/library/vstudio/ms173157.aspx[^],
http://msdn.microsoft.com/en-us/library/vstudio/44a9ty12.aspx[^].

—SA


It is not Possible, to do that task should create interface individually for different class.

Interface I1
       {
           method1()
       }
 
Interface I2
       {
           method2()
       }
       Class A : I1
       {
          \\ can Access only method1 not method2
       }
 
       Class B : I2
       {
          \\ can Access only method2 not method1
       }


这篇关于限制类以仅访问接口中的某些特定方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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