2012-03-10 87 views
12

可能重复:
What is the difference between 'protected' and 'protected internal'?
What is the difference between Public, Private, Protected, and Nothing?困惑:内部,保护,受保护的内部

代码是如下所述:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace testanotherlib 
{ 
    public class A 
    { 
     internal void InternalDisplay() 
     { 
      Console.WriteLine("Internal Display Method."); 
     } 

     protected void ProtectedDisplay() 
     { 
      Console.WriteLine("Protected Display Method."); 
     } 

     protected internal void ProtectedInternalDisplay() 
     { 
      Console.WriteLine("ProtectedInternal Display Method."); 
     } 

     public void PublicDisplay() 
     { 
      Console.WriteLine("Public Display Method."); 
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace testanotherlib 
{ 
    public class B : A 
    { 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using testanotherlib; 
namespace testlib 
{ 
    public class C:A 
    { 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using testlib; 
using testanotherlib; 

namespace testapp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      B objB = new B(); 
      C objC = new C(); 
     } 
    } 
} 

我想了解In之间的区别内部,受保护和受保护的内部。为此,我使用上面的代码创建了一个示例。

在类库项目testanotherlib我有类A & B类。在类库项目testlib我有类C.程序类是在一个单独的控制台应用程序。在Program类的主要方法内部,我为B类(objB)和C类(objC)创建了对象。对于objB和objC,只能使用类A的公共方法。我预计B班的所有方法都可以访问。请帮助我理解这一点。如果您需要关于该项目的任何其他信息,请随时询问我。

问候, Priyank

+0

你在哪里期待能够获得A类的所有方法,具有A级的参考?你的代码从来没有试图*使用*成员,这使得很难谈论... – 2012-03-10 14:11:13

+0

@JonSkeet:我期待能够访问所有的方法,如果类A,参考objB。 – 2012-03-10 14:14:34

+1

@PriyankThakkar:从'testApp'? *为什么*你期待那样?例如'testApp'中的代码与'A'不在同一个程序集中,因此任何内部成员都不可见。 – 2012-03-10 14:16:36

回答

12

以下五个无障碍水平可以使用访问修饰符来指定:

市民:访问不受限制。

保护:访问限于从包含类派生的包含类或类型。

内部:访问权限于当前程序集。

受保护的内部:访问受限于当前程序集或派生自包含类的类型。

private:Access仅限于包含类型。

Taken directly from Microsoft's MSDN library.

+0

你给'受保护的内部'定义看起来不准确,因为'或'。你确定它不应该是'和'吗? – ciuncan 2013-07-28 17:45:12

+5

你可能是对的。请立即通知微软! – 2013-07-28 20:31:03

+0

哦,我没有注意到链接,并懒得搜索。抱歉。而且似乎仍然是合乎逻辑的 – ciuncan 2013-07-28 21:28:40

5

internal

只有当前和友好的组件可见。

protected

继承 A类中

才可见。

protected internal

继承 A类中

可见。在当前和友好的程序集中也可见。

+0

嘿西蒙..感谢您的帮助:) – 2012-03-11 12:02:47

+0

受保护的内部手段受保护或内部;你将其描述为受保护的和内部的。 – Tuan 2015-07-07 18:47:18

4

protected方法和成员只能从派生自声明实际方法的类的另一个类访问。

class A 
{ 
    protected void Method() {} 
} 

class B : A 
{ 
    public void Foo() 
    { 
     Method(); // works! 
    } 
} 

class C 
{ 
    public void Foo() 
    { 
     Method(); // won't work, obviously 

     var tmp = new A(); 
     tmp.Method(); // won't work either because its protected 
    } 
} 

internal使该方法只在同一个程序集中可见。对于同一个程序集中的类,该方法可以像公开一样使用。对于你目前认为的私人类以外的课程。

现在,将受保护的和内部结合起来,可以在该程序集中的所有类的相同程序集中使用该方法。而且,受保护的方法使得该方法可以在所有派生类中使用,而不管哪个程序集。

+0

你得到了内部保护的错误,看到我的答案或克里斯'的事。 – Terkel 2012-03-10 14:15:34

+0

@SimonBangTerkildsen哦,你是对的。我纠正了我的答案。不知道。那么,从来没有用过它。所以内部有效地推翻了受保护的关键字。派生时忽略内部。不知道,实际上并不喜欢它:)但是这一定有一个很好的理由。 – dowhilefor 2012-03-10 15:13:01

+0

@dowhilefor:谢谢你用精彩的例子来解释:) :) – 2012-03-11 12:01:39