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

时间:2018-04-16 08:22:48

标签: c# inheritance abstract

我想在具有抽象类类型的对象上调用子类实现。但是,这并没有像我想象的那样工作。 有没有办法做到这一点,并不要求我在第二个开关声明中切换类型?或者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() { ... }
}

实际的方法确实有参数,我确实想要一些方法的基础实现(但不是所有方法)。目标是允许入学课程扩展'方法行为。

2 个答案:

答案 0 :(得分:2)

尝试使用virtual keyword

使用虚拟时,您可以在基类中为方法提供默认值'实现。像这样:

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()。如果你愿意,你甚至可以给这个方法一个全新的含义。

答案 1 :(得分:2)

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

以下代码显示了使用抽象类时的各种选项,而不是所有选项。

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