多态性,继承和泛型C#:无法将子类强制转换为超类型

时间:2016-08-04 08:44:49

标签: c# generics inheritance interface covariance

我在c#中有抽象类和泛型的问题(我通常用Java编写代码):这是我想要使用的代码:

public interface InterfaceResult {...}

public abstract class Result : InterfaceResult {...}   
public class ResultA : Result {...}
public class ResultB : Result {...}

public interface InterfaceKing { InterfaceResult function();}

public abstract class King : InterfaceKing {
  public abstract Result function();
}
public class KingA : King {
  public override ResultA function(){...}
}
public class KingB : King {
  public override ResultB function(){...}
}

但这不起作用:Visual Studio希望KingA和KingB使用function()返回Result的实例。如果我使用“new”而不是override,VS说我没有实现所需的方法(我需要使用覆盖)。

所以我尝试使用泛型而且不是更好

public abstract class Result    {    }
public class ResultA : Result    {    }
public class ResultB : Result    {    }

public interface IKing<T>    {T function();    }

public abstract class King<Result> : IKing<Result>
{
    public abstract Result function();

    public static implicit operator King<Result>(KingB v)
    {
        return v; // generate StackOverflow Exception
    }

    public static implicit operator King<Result>(KingA v)
    {
        throw new NotImplementedException();
    }
}

public class KingA : King<ResultA>
{
    public override ResultA Get()
    {
        return new ResultA();
    }
}

public class KingB : King<ResultB>
{
    public override ResultB Get()
    {
        return new ResultB();
    }
}

public class Test
{
    King<Result> a = new KingA(); // allowed by  public static implicit operator King<Result>(KingA v)
    King<Result> b = new KingB(); // allowed by  public static implicit operator King<Result>(KingB v)
    KingA ka = new KingA();
    List<King<Result>> lista = new List<King<Result>>();
    public void test()
    {
        lista.Add(ka);
    }        
}

我怎样才能做到这一点?我找不到任何解决方案,也没有任何好的或完整的例子。

2 个答案:

答案 0 :(得分:1)

从&#34;完整界面移动&#34;到&#34;没有界面&#34; (缺乏更好的条款)所有工作(这个主要工作正常)

static void Main ()
{
    King a = new KingA ();
    King b = new KingB ();
    KingA ka = new KingA ();
    List<King> list = new List<King> ();

    list.Add (a);
    list.Add (b);
    list.Add (ka);
}

&#34;完整界面&#34;

    public interface InterfaceResult { }

    public abstract class Result : InterfaceResult { }
    public class ResultA : Result { }
    public class ResultB : Result { }

    public interface InterfaceKing { InterfaceResult Function (); }

    public abstract class King : InterfaceKing
    {
        public abstract InterfaceResult Function ();
    }
    public class KingA : King
    {
        public override InterfaceResult Function () => new ResultA ();
    }
    public class KingB : King
    {
        public override InterfaceResult Function () => new ResultA ();
    }

&#34;混合(界面较少)&#34;

    public abstract class Result { }
    public class ResultA : Result { }
    public class ResultB : Result { }

    public interface IKing { Result Function (); }

    public abstract class King : IKing
    {
        public abstract Result Function ();
    }

    public class KingA : King
    {
        public override Result Function () => new ResultA ();
    }

    public class KingB : King
    {
        public override Result Function () => new ResultB ();
        {
            return new ResultB ();
        }
    }

&#34;没有界面&#34;

    public abstract class Result { }
    public class ResultA : Result { }
    public class ResultB : Result { }

    public abstract class King
    {
        public abstract Result Function ();
    }

    public class KingA : King
    {
        public override Result Function () => new ResultA ();
    }

    public class KingB : King
    {
        public override Result Function () => new ResultB ();
    }

答案 1 :(得分:0)

使用协方差

请参阅.net fiddler here。详细了解Covariance and Contravariance

public abstract class Result { }

public class ResultA : Result { }

public class ResultB : Result { }

public interface IKing<out T> where T : Result {}

public abstract class King<T> : IKing<T> where T : Result
{
    public abstract T Get();
}

public class KingA : King<ResultA>
{
    public override ResultA Get()
    {
        return new ResultA();
    }
}

public class KingB : King<ResultB>
{
    public override ResultB Get()
    {
        return new ResultB();
    }
}

public class TestClass
{
    King<ResultA> a = new KingA(); // allowed by  public static implicit operator King<Result>(KingA v)
    King<ResultB> b = new KingB(); // allowed by  public static implicit operator King<Result>(KingB v)
    KingA ka = new KingA();
    List<IKing<Result>> lista = new List<IKing<Result>>();

    public void Test()
    {
        lista.Add(ka);
    }        
}