可能重複:
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
你在哪裏期待能夠獲得A類的所有方法,具有A級的參考?你的代碼從來沒有試圖*使用*成員,這使得很難談論... – 2012-03-10 14:11:13
@JonSkeet:我期待能夠訪問所有的方法,如果類A,參考objB。 – 2012-03-10 14:14:34
@PriyankThakkar:從'testApp'? *爲什麼*你期待那樣?例如'testApp'中的代碼與'A'不在同一個程序集中,因此任何內部成員都不可見。 – 2012-03-10 14:16:36