简体   繁体   English

虚拟 class 中的抽象方法

[英]abstract method in a virtual class

I have a c# Class that has lots of virtual methods, some of these methods are essentially abstract ( they are fully implemented in subclasses and the base class is empty).我有一个 c# Class 有很多虚拟方法,其中一些方法本质上是抽象的(它们在子类中完全实现,基础 class 是空的)。

To get it to compile i am throwing an InvalidOperationException in the base class with a comment on what should be done.为了让它编译,我在基础 class 中抛出一个 InvalidOperationException 并评论应该做什么。 This just feels dirty.这只是感觉很脏。

Is there a better way to design my classes?有没有更好的方法来设计我的课程?

edit: It is for the middle tier of an application that will be ran in canada, half of the methods are generic hence the virtual.编辑:它用于将在加拿大运行的应用程序的中间层,一半的方法是通用的,因此是虚拟的。 and half of the methods are province specific.一半的方法是针对特定省份的。

Public class PersonComponent() 
{

 public GetPersonById(Guid id) {
  //Code to get person - same for all provinces
 }

 Public virtual DeletePerson(Guid id) {
  //Common code
 }

 Public virtual UpdatePerson(Person p) {
  throw new InvalidOperation("I wanna be abstract");
 }

Public Class ABPersonComponent : PersonComponent
{
    public override DeletePerson(Guid id) 
    {
       //alberta specific delete code
    }

    public override UpdatePerson(Person p) 
    {
       //alberta specific update codecode
    }

}

hope this makes sense希望这是有道理的

Mark the base class as abstract, as well as the methods that have no implementation.将基础 class 标记为抽象,以及没有实现的方法。

Like so像这样

public abstract class BaseClass
{

    public abstract void AbstractMethod();
}

public class SubClass: BaseClass
{
    public override void AbstractMethod()
    {
        //Do Something
    }
}

You can't have abstract methods outside of an abstract class.您不能在抽象 class 之外拥有抽象方法。 Marking a class as abstract means you won't be able to instantiate it.将 class 标记为抽象意味着您将无法实例化它。 But then it doesn't make any sense to.但这没有任何意义。 What are you going to do with a class that doesn't implement the methods anyway?你打算怎么处理一个 class 无论如何都没有实现这些方法?

Edit: From looking at your class, yeah I'd make PersonComponent abstract along with the UpdatePerson method.编辑:通过查看您的 class,是的,我将 PersonComponent 与 UpdatePerson 方法一起抽象。 Either that, or if UpdatePerson just doesn't do anything for a PersonComponent keep it as is, but make the UpdatePerson method empty for PersonComponent.或者,如果 UpdatePerson 只是不为 PersonComponent 做任何事情,请保持原样,但将 PersonComponent 的 UpdatePerson 方法设为空。

Think about your object hierarchy.想想你的 object 层次结构。 Do you want to share common code for all your derived classes, then implement base functionality in the base class.您想共享所有派生类的通用代码,然后在基础 class 中实现基础功能。

When having shared base code, please notice the Template pattern.共享基本代码时,请注意模板模式。 Use a public method and chain it to a protected virtual method with the core/shared implementation.使用公共方法并将其链接到具有核心/共享实现的受保护虚拟方法。 End the shared implementation methodname with "Core".以“Core”结束共享实现方法名。

For example:例如:

public abstract class BaseClass
{
    protected virtual void DeletePersonCore(Guid id)
    {
        //shared code
    }

    public void DeletePerson(Guid id)
    {
        //chain it to the core
        DeletePersonCore(id);
    }
}

public class DerivedClass : BaseClass
{
    protected override void DeletePersonCore(Guid id)
    {
        //do some polymorphistic stuff

        base.DeletePersonCore(id);
    }
}

public class UsageClass
{
    public void Delete()
    {
        DerivedClass dc = new DerivedClass();

        dc.DeletePerson(Guid.NewGuid());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM