我想重写一个抽象方法,但要保持它是抽象的,所以派生类需要自己实现它。
public abstract class ComponentController
{
public abstract void CreateCustomColumns();
}
public abstract class ClientComponentController : ComponentController
{
public override void CreateCustomColumns()
{
// All grids to do with clients should show these columns.
Grid.AddColumn("Client" ...
Grid.AddColumn("ClientLocation" ...
Grid.AddColumn("ClientPhoto" ...
}
}
public class ClientInvoicesComponentController : ClientComponentController
{
public override void CreateCustomColumns()
{
base.CreateCustomColumns();
// On top of the generic columns, I also want to show these.
Grid.AddColumn("InvoiceNumber" ...
Grid.AddColumn("InvoiceDate" ...
}
}
public class ClientCommunicationsComponentController : ClientComponentController
{
public override void CreateCustomColumns()
{
base.CreateCustomColumns();
// On top of the generic columns, I also want to show these.
Grid.AddColumn("CommunicationDate" ...
Grid.AddColumn("CommunicationType" ...
}
}
在此代码中,ClientInvoicesComponentController 不需要实现 CreateCustomColumns(),因为这是不允许的:
public abstract class ClientComponentController : ComponentController
{
public abstract override void CreateCustomColumns()
{
Grid.AddColumn("Client" ...
Grid.AddColumn("ClientLocation" ...
Grid.AddColumn("ClientPhoto" ...
}
}
--> "Abstract method cannot declare a body"
那么,如何在 ClientComponentController 中覆盖 CreateCustomColumns(),但仍然强制在 ClientInvoicesComponentController 等派生类中再次覆盖它?
当然,无论如何它仍然可以被覆盖,但是没有任何东西向开发人员表明它必须被覆盖......这是我的目标。
-布伦丹