「コンポーネント」クラスに保護された仮想メソッドを追加して、「コンポジット」から呼び出すことができるようにするにはどうすればよいですか?
具体例として、以下のコードを見て、 のコンパイルエラーを回避する方法を教えてくださいDxCompositeShape.ComputeSize
。
abstract class DxShape // this is the Component
{
public abstract void Paint();
protected abstract void ComputeSize();
}
class DxCompositeShape : DxShape // this is the Composite
{
public readonly IList<DxShape> Shapes = new List<DxShape>();
public override void Paint()
{
this.ComputeSize();
}
protected override void ComputeSize()
{
foreach (DxShape sh in Shapes)
{
sh.ComputeSize(); // compiler error CS1540
}
// and some other logic here
}
}
編集:サンプルを変更したので、ComputeSize
代わりにInit
(コンストラクターで Init を常に呼び出すことができると人々は想定しています)。