为什么公共类不能继承不太显眼的类?
可能的重复:
C#:为什么我的公共类不能扩展内部类类?
如果之前有人问过这个问题,我深表歉意。我已经搜索过一些但无法找到它。
我只是好奇这个设计背后的基本原理是什么。显然,我知道基类型的私有/内部成员不能也不应该通过派生的公共类型公开。但在我看来,似乎天真的想法是,“隐藏”部分很容易保持隐藏状态,而一些基本功能仍然是共享的,并且新的界面是公开的。
我正在考虑这样的事情:
Assembly X
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?
}
}
Assembly Y
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?
}
Possible Duplicate:
C#: Why can't my public class extend an internal class?
I apologize if this question has been asked before. I've searched SO somewhat and wasn't able to find it.
I'm just curious what the rationale behind this design was/is. Obviously I understand that private/internal members of a base type cannot, nor should they, be exposed through a derived public type. But it seems to my naive thinking that the "hidden" parts could easily remain hidden while some base functionality is still shared and a new interface is exposed publicly.
I'm thinking of something along these lines:
Assembly X
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?
}
}
Assembly Y
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?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您真正要求的是一种受限制的非公共继承形式。在 .NET 中,基类被视为接口的一部分,而不是实现细节。如果您想在实现中重用另一个类,您当然可以使用组合而不是继承。
另请注意,允许实现可见性/可访问性较低的接口。您所询问的限制仅适用于基类。
What you're really asking for is a restricted form of non-public inheritance. In .NET, the base class is considered part of the interface, not an implementation detail. If you want to reuse another class in your implementation, you can of course use composition instead of inheritance.
Also note that implementing an interface with lesser visibility/accessibility IS allowed. The restriction you're asking about applies to base classes only.
当一个类从基类继承时,基类成员就成为继承类的一部分。为了维持这个概念,基类的可见性范围必须等于或优于继承类。
When a class inherits from a base class, the base members become part of the inherted class. In order to maintain this concept, the visibility scope of the base class must be equal or better than the inhertied class.
我对内部的理解是这样的。
让我创建一个系统。
在该系统中,您正在 College-X 学习,并且您有我创建的 2.0 内部考试。
我将该试卷标记为内部试卷。
我可以将试卷
公开
- 将其发布在互联网受保护
中 - 将其发布在教师邮件中组
私人
- 将其放置在瑞士金库中假设拥有 5.0 博士学位的 College-Y 的两名学生(例如乔恩和埃里克)也在该系统中。
试卷(对象)与他们无关。
即使它在互联网(公共)上,他们也不应该使用它,因为它对他们没有用。
这就是为什么我最初将其标记为内部。
否则为什么要有一个名为
internal
的access-modifier
呢?I understand internal like this.
Let me create a system.
In that system, you are studying at College-X and you have an internal exam on 2.0 that I created.
I mark that question paper as internal.
I can make the question paper
public
- post it in internetprotected
- post it in faculty mailgroup
private
- place it at a swiss vaultSuppose two students at College-Y, say Jon and Eric, with PhD in 5.0 are also in this system.
The question paper(object) is irrelevant to them.
Even if it's on internet (public), they should not be able to use it since its of no use to them.
Thats why I marked it internal initially.
Otherwise why should there be an
acceess-modifier
calledinternal
?