简体   繁体   English

强制儿童班实施会员班

[英]Force child class to implement member class

First of all, I would like to say that I am mainly interested in if the following is possible, and I do not intent to actually implement it anywhere soon, and as such bad design is not relevant at this moment. 首先,我想说的是,我主要感兴趣的是是否可以进行以下操作,并且我不打算很快在任何地方实际实施它,因为这种不良设计目前不相关。 I understand this is easily achieved by using a virtual function. 我了解这可以通过使用虚函数轻松实现。

Question explanation 问题说明

The following is an example of what I am trying to achieve. 以下是我要实现的示例。 It is by no means correct C#. 绝不是正确的C#。

Say I have a class Foo which contains an abstract class Bar , which then contains a function Foobar . 假设我有一个Foo类,其中包含一个抽象类Bar ,然后该类包含一个功能Foobar Class Foo uses this Foobar function somewhere in a protected function. Foo类在受保护的函数中的某个位置使用此Foobar函数。 This is ofcourse not correct C#, as you cannot create an instance of an abstract class. 当然这不是正确的C#,因为您无法创建抽象类的实例。

Class Foo
{
    protected abstract class Bar
    {
        public void Foobar();
    }

    public void RandomFunction()
    {
        Bar bar = new Bar();
        bar.Foobar();
    }
}

I now create a child class of Foo , named FooChild . 现在,我创建一个名为FooChildFoo子类。 Since Bar is abstract, FooChild is forced to override this class. 由于Bar是抽象的,因此FooChild被强制重写此类。

Class FooChild : Foo
{
    override class Bar
    {
        public void Foobar()
        {
            Console.WriteLine("This is FooChild.");
        }
    }
}

Say there is a different part of this program, which requires an instance of class Foo . 假设该程序有另一个部分,需要一个Foo类的实例。

void UseFoo(Foo myFoo)
{
    myFoo.RandomFunction();
}

He gets this class via some other function. 他通过其他函数来获得此类。 However, since FooChild is a child of Foo, it can be passed to the function instead. 但是,由于FooChild是Foo的子级,因此可以将其传递给函数。

UseFoo(new FooChild());

The expected outcome of this program would be: 该计划的预期结果将是:

This is FooChild. 这是FooChild。

My question is, what the correct syntax would be for this, or instead a reasoning why this is not possible. 我的问题是,正确的语法是什么,或者是为什么这不可能的原因。

It's important to remember that C# nested types are a static construct. 请记住, C#嵌套类型是静态构造,这一点很重要。 So the abstract Bar nested in Foo does not have any real bearing on subclassing Foo - the situation would be the same if you instead declared Bar outside of Foo (other than the fact it's nested). 所以抽象Bar嵌套在Foo没有在子类任何真正的轴承Foo -的情况是相同的,如果你不是宣称BarFoo (比实际上它是嵌套等)。

This is slightly confused by the fact that when you subclass Foo with FooChild , you inherit FooChild.Bar which is the same as Foo.Bar - but this is static member inheritance, therefore only really a matter of scope. 当您用FooChild继承Foo子类时,您继承了FooChild.Bar ,这与FooChild相同,这Foo.Bar -但这是静态成员继承,因此实际上仅是范围问题。

Now removing the nesting of Bar , your question really boils down to the line you notice is invalid C#: 现在删除Bar的嵌套,您的问题实际上可以归结为您发现无效的C#行:

    Bar bar = new Bar();

since Bar is abstract, somebody will need to subclass Bar in order to create something of that type. 由于Bar是抽象的,因此有人需要继承Bar子类才能创建该类型的东西。 You might arrange for this to be passed in from the outside, or created directly in your subclass. 您可以安排将其从外部传递或直接在您的子类中创建。 Or if you want to force the subclass to also subclass Bar you could use an abstract factory method on Foo : 或者,如果您想强制子类也将Bar子类化,则可以在Foo上使用抽象工厂方法:

abstract class Foo
{
    public abstract Bar CreateBar();
    public void RandomFunction()
    {
        Bar bar = CreateBar();
        bar.Foobar();
    }
}

Notice that whether your subclass creates the derived Bar subclass as a nested class or at the top level is irrelevant to this design. 请注意,您的子类是将派生的Bar子类创建为嵌套类还是在顶层创建与该设计无关。

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

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