简体   繁体   English

C#-高级继承

[英]C# - advanced inheritance

Please take a look at my class structure. 请看一下我的课程结构。 I think I would like to have more fun with inheritance than it is possible. 我想我想对继承有更多的乐趣。

First there is a base abstract class: 首先,有一个基本的抽象类:

public abstract class PolicyDetailed
{
    internal abstract DataContainer GetActiveAsset();
}

Next there is another abstract class, which is generic: 接下来是另一个抽象类,它是通用的:

public abstract class PolicyDetailed<T> : PolicyDetailed where T : DataContainer
{
    internal new abstract T GetActiveAsset();
}

Lastly there is a specific policy class. 最后,有一个特定的策略类。 AccidentContainer inherits from DataContainer: AccidentContainer继承自DataContainer:

public class PolicyAccident : PolicyDetailed<AccidentContainer>
{
    internal override AccidentContainer GetActiveAsset()
    {
        return null;
    }
}

During compilation I get the following error: 在编译期间,出现以下错误:

'PolicyAccident' does not implement inherited abstract member 'PolicyDetailed.GetActiveAsset()'  

I am not sure what modifiers I should use here to get it to work. 我不确定应该在这里使用哪些修饰符使其生效。 Maybe I should also write what I would like to achieve: I have a collection of policy objects of different type (eg PolicyAccident, PolicyTravel etc.) which inherit from PolicyDetailed with different types of DataContainer (AccidentContainer, TravelContainer etc.). 也许我也应该写我想实现的目标:我有一组不同类型的策略对象(例如PolicyAccident,PolicyTravel等),这些对象从PolicyDetails继承并带有不同类型的DataContainer(AccidentContainer,TravelContainer等)。 I would like to call "GetActiveAsset" method on each of them without knowing their specific type and referencing them through PolicyDetailed. 我想在不知道它们的特定类型并通过PolicyDetailed引用它们的情况下,在每个函数上调用“ GetActiveAsset”方法。 At the same time I would like each class to return their specific Datacontainer subclass. 同时,我希望每个类都返回其特定的Datacontainer子类。 Is that possible? 那可能吗?

The problem is that you can't override the non-generic method in the same class as you declare any other method with the same signature. 问题是您不能在声明具有相同签名的任何其他方法时覆盖同一类中的非泛型方法。

There are a few options: 有几种选择:

  • By far the simplest is to give the two methods different names. 到目前为止,最简单的方法是为这两种方法指定不同的名称。 Then you can give an implementation in PolicyDetailed<T> which just delegates to the new abstract method: 然后,您可以在PolicyDetailed<T>给出一个实现,该实现只委托给新的抽象方法:

     public abstract class PolicyDetailed { internal abstract DataContainer GetActiveAsset(); } public abstract class PolicyDetailed<T> : PolicyDetailed where T : DataContainer { internal abstract T GetActiveAssetGeneric(); internal override DataContainer GetActiveAsset() { return GetActiveAssetGeneric(); } } public class PolicyAccident : PolicyDetailed<AccidentContainer> { internal override AccidentContainer GetActiveAssetGeneric() { return null; } } 
  • You could introduce another level of inheritance, introducing a new method name just for bridging purposes. 可以引入另一个继承层次,引入一个用于桥接目的的新方法名称。 This is pretty ugly: 这很丑陋:

     public class DataContainer {} public class AccidentContainer : DataContainer{} public abstract class PolicyDetailed { internal abstract DataContainer GetActiveAsset(); } // This only exists to satisfy the base class abstract member, // but at the same time allowing PolicyDetailed<T> to introduce // a new member with the same name. public abstract class PolicyDetailedBridge<T> : PolicyDetailed where T : DataContainer { protected abstract T GetActiveAssetGeneric(); internal override DataContainer GetActiveAsset() { return GetActiveAssetGeneric(); } } public abstract class PolicyDetailed<T> : PolicyDetailedBridge<T> where T : DataContainer { protected sealed override T GetActiveAssetGeneric() { // Call the *new* abstract method. Eek! return GetActiveAsset(); } internal abstract new T GetActiveAsset(); } public class PolicyAccident : PolicyDetailed<AccidentContainer> { internal override AccidentContainer GetActiveAsset() { return null; } } 
  • You could make the non-generic PolicyDetailed class an interface instead, and use explicit interface implementation to declare a new abstract method and still implement the interface. 您可以改为使非泛型PolicyDetailed类成为接口,并使用显式接口实现来声明新的抽象方法并仍然实现该接口。

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

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