Inheriting SplitterPanel
本文关键字:SplitterPanel Inheriting | 更新日期: 2023-09-27 18:01:45
我有以下c#类:
class BufferedSplitPanel : SplitterPanel
{
public BufferedSplitPanel()
{
this.DoubleBuffered = true;
}
}
但是编译器(和智能感知)告诉我this
没有这样的成员DoubleBuffered
(或任何其他成员)。MSDN清楚地说明了这一点,因为它继承自Panel。如果我把SplitterPanel
改成Panel
,它就编译了。我做错了什么?
SplitterPanel
是sealed
类,而Panel
不是。
你不能从sealed
类型派生。
文档:
密封类不能被继承。使用密封类作为基类是错误的。在类声明中使用sealed修饰符可以防止类的继承。
如果你想扩展sealed
类的功能,最好的方法是创建扩展方法。例如:
public static class SplitterPanelExtensions {
public static void MyAdvancedMethod(this SplitterPanel splitterPanel) {
/*
* Check if splitterPanel is null and throw ArgumentNullException.
* because extension methods are called via "call" IL instruction.
*/
//Implementation.
}
//Other extension methods...
}
另一种方法是创建一个类来保存密封类的实例。如果您想隐藏正在包装的类的接口的某些部分,这是更好的选择。例如:
public class SplitterPanelWrapper {
private readonly SplitterPanel m_SplitterPanel;
public SplitterPanelWrapper(SplitterPanel splitterPanel) {
m_SplitterPanel = splitterPanel;
}
//Other implementation.
}
我在MSDN文档中看不到任何DoubleBuffered属性。你确定你需要那处房产吗?使用它似乎也没有多大意义。
除此之外,MSDN还说这个类是密封的。你不能从它推导出来。你应该得到编译错误。