2012-03-10 61 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

相關問題