C#有私有和保护继承的概念? [英] Does C# have the notion of private and protected inheritance?
问题描述
C#有私人/保护继承的概念,如果没有,为什么?
Does C# have the notion of private / protected inheritance, and if not, why?
C ++
class Foo : private Bar {
public:
...
};
C#
public abstract NServlet class : private System.Web.UI.Page
{
// error "type expected"
}
我实施的servlet像概念一个.aspx页面,我不想具体类有看System.Web.UI.Page基地内部的能力。
I am implementing a "servlet like" concept in an .aspx page and I don't want the concrete class to have the ability to see the internals of the System.Web.UI.Page base.
推荐答案
C#只允许公有继承。 C ++允许的所有三种。公有继承所隐含的IS-A型关系,而私有继承意味着A是实现多功能条件方面的一种关系。由于分层(或组合)来实现这一点的可以说是比较简单的方式,只有当经保护成员或虚函数绝对必要要求它使用私有继承 - 根据斯科特迈尔斯在有效的C ++项目42
C# allows public inheritance only. C++ allowed all three kinds. Public inheritance implied an "IS-A" type of relationship, and private inheritance implied a "Is-Implemented-In-Terms-Of" kind of relationship. Since layering (or composition) accomplished this in an arguably simpler fashion, private inheritance was only used when absolutely required by protected members or virtual functions required it - according to Scott Meyers in Effective C++, Item 42.
我的猜测是,C#的作者没有感觉的另一个方面实现一个类是必要的这个额外的方法。
My guess would be that the authors of C# did not feel this additional method of implementing one class in terms of another was necessary.
这篇关于C#有私有和保护继承的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!