繁体   English   中英

调用抽象类类型C#的子实现

[英]Call child implementation of abstract class type C#

我想在具有抽象类类型的对象上调用子类实现。 但是,这不符合我的预期。 有没有办法做到这一点,不需要我在第二个开关空间中在类型之间进行切换? 还是C#不允许这种行为?

被称为的代码:

AbstractParentType wfp;

//Switch on diagram type and select processor
switch (qi.DIAGRAMTYPE)
{
    case 1:
        wfp = new T1(notifications);
        break;
    case 2:
        wfp = new T2(notifications);
        break;
    case 3:
        wfp = new T3(notifications);
        break;
    default:
        throw new Exception("Diagramtype not implemented");
}

bool result = false;
//Switch on action type
switch (qi.Type)
{
    case (int)WorkflowActionType.BelItem:
        //Do some case specific stuff here
        ...
        //Call method
        result = wfp.Meth1();
        break;
    ... (a bunch of cases) ...
    case (int)WorkflowActionType.WordDocument:
        //Do some case specific stuff here
        ...
        //Call method
        result = wfp.Meth10();
        break;
}

然后,我们有了类的实现:

abstract class AbstractClassType {
     public bool Meth1() { ... }
     ...
     public bool Meth10() { ... }
     ...
     public abstract MethX();
     public abstract MethY();
}

class T1 : AbstractClassType  {
     public new Meth1() { ... }
     ...
     public new Meth10() { ... } 
     ...
     public override MethX() { ... }
     public override MethY() { ... }
}

实际的方法确实具有参数,我确实想要某些方法(但不是全部)的基本实现。 目的是允许继承的类“扩展”方法的行为。

尝试使用virtual关键字

使用虚拟时,可以为基类中的方法提供“默认”实现。 像这样:

abstract class AbstractClassType {
    public virtual void MethX(){
        //default implementation here.            
    }
    public virtual void MethY(){
        //another default implementation here!
    }
}

class T1 : AbstractClassType {
    public override void MethX(){
        //base.MethX() would call the logic in the base class. 
    }
    public override void MethY(){ 
        //base.MethY() would call the logic in the base class. 
    }
}

virtualabstract之间的区别在于,基本上, abstract方法不能具有基本实现,而必须被覆盖。

virtual方法可以具有基本实现,并且不需要被覆盖。

您不需要调用base.MethX/Y() 如果愿意,您甚至可以给该方法一个全新的含义。

首先,您不能创建抽象类的对象,因为它实际上不是完整的实体。 您将始终需要实例化扩展抽象类的类的对象。

以下代码显示了使用抽象类时可以使用的各种(不是全部)选项。

    public abstract class AbstractClass
    {
        public void OnlyInAbstract() {
            Console.WriteLine("You are stuck with OnlyInAbstract in abstract class unless you use new keyword.");
        }

        public virtual void OnlyInAbstractForNow()
        {
            Console.WriteLine("You have reached abstract class for now. However, override me for changed behaviour.");
        }

        public abstract void MustImplement();
    }

    public class FirstChild : AbstractClass
    {
        public override void MustImplement()
        {
            Console.WriteLine("You called MustImplement in FirstChild. Nothing else to see here.");
        }

        public override void OnlyInAbstractForNow()
        {
            base.OnlyInAbstractForNow();
            Console.WriteLine("I see you changed my behaviour in FirstChild to extend it after abstract class was done with.");
        }

        public new void OnlyInAbstract()
        {
            Console.WriteLine("Looks like we are making an all new OnlyInAbstract method in child class.");
        }
    }

    static void Main(string[] args)
    {
        AbstractClass abstractClass = new FirstChild();

        abstractClass.MustImplement();

        abstractClass.OnlyInAbstract();

        (abstractClass as FirstChild).OnlyInAbstract();

        abstractClass.OnlyInAbstractForNow();

        Console.Read();
    }

暂无
暂无

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

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