简体   繁体   English

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

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

I want to call the child class implementation on an object with the abstract class type. 我想在具有抽象类类型的对象上调用子类实现。 However, this doesn't work the way I thought it would. 但是,这不符合我的预期。 Is there a way to do it, that doesn't require me to switch between types in the second switch satement? 有没有办法做到这一点,不需要我在第二个开关空间中在类型之间进行切换? Or does C# not allow for this type of behavior? 还是C#不允许这种行为?

The code where it is called: 被称为的代码:

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;
}

Then we have the classes implementations: 然后,我们有了类的实现:

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() { ... }
}

The actual methods do have parameters and I do want a base implementation for some of the methods (but not all of them). 实际的方法确实具有参数,我确实想要某些方法(但不是全部)的基本实现。 The goal is to allow the inherting classes to 'extend' the methods behavior. 目的是允许继承的类“扩展”方法的行为。

Try using the virtual keyword 尝试使用virtual关键字

When using virtual, you can give methods in the base class a 'default' implementation. 使用虚拟时,可以为基类中的方法提供“默认”实现。 Like so: 像这样:

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. 
    }
}

The difference between virtual and abstract is that basically, an abstract method cannot have a base implementation, and must be overridden. virtualabstract之间的区别在于,基本上, abstract方法不能具有基本实现,而必须被覆盖。

A virtual method can have a base implementation, and does not need to be overriden. virtual方法可以具有基本实现,并且不需要被覆盖。

You're not required to call base.MethX/Y() . 您不需要调用base.MethX/Y() You could even give the method a whole new meaning if you wanted to. 如果愿意,您甚至可以给该方法一个全新的含义。

First of all, you cannot create an object of an abstract class as it is not really a complete entity. 首先,您不能创建抽象类的对象,因为它实际上不是完整的实体。 You will always need to instantiate the object of a class that extends the abstract class. 您将始终需要实例化扩展抽象类的类的对象。

Following code shows various, not all, options you have when working with abstract classes. 以下代码显示了使用抽象类时可以使用的各种(不是全部)选项。

    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