【发布时间】:2012-03-27 16:06:39
【问题描述】:
可能重复:
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();
}
}
}
我试图了解内部、受保护和受保护内部之间的区别。为此,我使用上面的代码创建了一个示例。
在类库项目 testanotherlib 中,我有 A 类和 B 类。在类库项目 testlib 中,我有 C 类。程序类位于单独的控制台应用程序中。在 Program 类的 main 方法中,我为 B 类(objB)和 C 类(objC)创建了对象。对于 objB 和 objC,只能访问类 A 的公共方法。我预计 B 类的所有方法都可以访问 A 类的所有方法。请帮助我理解这一点。如果您需要有关该项目的任何其他信息,请随时问我。
问候, 普里扬克
【问题讨论】:
-
您期望在哪里能够访问 A 类的所有方法,并引用 A 类?您的代码从不尝试使用成员,这使得谈论起来很困难......
-
@JonSkeet:如果是 A 类,我希望能够通过引用 objB 访问所有方法。
-
@PriyankThakkar:来自
testApp? 为什么是你期待的?testApp中的代码与A不在同一个程序集中,因此例如,任何内部成员都不可见。 -
微软为什么不在 MSDN 中提供 PROTECTED INTERNAL 的主题?