Dan*_*Tao 7 .net c# inheritance access-modifiers
可能重复:
C#:为什么我的公共类不能扩展内部类?
如果以前曾问过这个问题,我道歉.我有点搜索,无法找到它.
我只是好奇这个设计背后的理由是什么.显然,我理解基类型的私有/内部成员不能,也不应该通过派生的公共类型公开.但我天真的想法似乎是,"隐藏"的部分可以很容易地隐藏起来,而一些基本功能仍然是共享的,并且新的界面是公开的.
我正在考虑这些方面的事情:
internal class InternalClass
{
protected virtual void DoSomethingProtected()
{
// Let's say this method provides some useful functionality.
// Its visibility is quite limited (only to derived types in
// the same assembly), but at least it's there.
}
}
public class PublicClass : InternalClass
{
public void DoSomethingPublic()
{
// Now let's say this method is useful enough that this type
// should be public. What's keeping us from leveraging the
// base functionality laid out in InternalClass's implementation,
// without exposing anything that shouldn't be exposed?
}
}
Run Code Online (Sandbox Code Playgroud)
public class OtherPublicClass : PublicClass
{
// It seems (again, to my naive mind) that this could work. This class
// simply wouldn't be able to "see" any of the methods of InternalClass
// from AssemblyX directly. But it could still access the public and
// protected members of PublicClass that weren't inherited from
// InternalClass. Does this make sense? What am I missing?
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1519 次 |
| 最近记录: |