如何让抽象方法返回具有具体实现的抽象类型?

发布于 2024-09-18 18:58:27 字数 784 浏览 7 评论 0原文

我有三个类,每个类都会返回略有不同的结果。

// interfact to a king
public interface IKing{
  public Result Get();
}

// main abstract class
public abstract class King:IKing{
  public abstract Result Get();
}

// main abstract result
public abstract class Result{
  public int Type {get;set;}
}

// KingA result
public class ResultA:Result{
   ...
}

// KingB result
public class ResultB:Result{
   ...
}

// concrete implementations
public class KingA:King{
   public override ResultA Get(){
     return new ResultA;
   }
}

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

这将不起作用,因为 GetKing 重写方法需要 Result 类,并且不会接受其子 ResultAResultB

我可以采取更好的方法吗?

I have three classes that will each return a slightly different result.

// interfact to a king
public interface IKing{
  public Result Get();
}

// main abstract class
public abstract class King:IKing{
  public abstract Result Get();
}

// main abstract result
public abstract class Result{
  public int Type {get;set;}
}

// KingA result
public class ResultA:Result{
   ...
}

// KingB result
public class ResultB:Result{
   ...
}

// concrete implementations
public class KingA:King{
   public override ResultA Get(){
     return new ResultA;
   }
}

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

This will not work since the King overriden method of Get is expecting the Result class and will not accept its children ResultA and ResultB.

Is there a better approach I can take?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慕烟庭风 2024-09-25 18:58:27

通常的方法是使用泛型。

public interface IKing<T> where T:Result{
    T Get();
}

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

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

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

编辑:固定语法。

The usual approach is to use generics.

public interface IKing<T> where T:Result{
    T Get();
}

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

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

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

Edit: fixed syntax.

看海 2024-09-25 18:58:27

如果您使用可编译的代码,将会有所帮助。您的“具体实现”是假的,看起来您混合了类和方法的概念。否则这里不存在设计问题。例如:

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

It will help if you use code that compiles. Your 'concrete implementations' are bogus, it looks like you mixed the concepts of class and method. There is otherwise no design problem here. For example:

public class KingA : King {
    public override Result Get() {
        return new ResultA();
    }
}
背叛残局 2024-09-25 18:58:27

我认为这里存在一些语法混乱——如果我正确地捕捉到了你的意图,那么这工作正常:(

// interface to a king
public interface IKing
{
    Result Get();
}

// main abstract class
public abstract class King : IKing
{
    public abstract Result Get();
}

// main abstract result
public abstract class Result
{
    private int _Type;
    public int Type { get { return _Type; } set { _Type = value; } }
}

// KingA result
public class ResultA : Result
{

}

// KingB result
public class ResultB : Result
{

}

// concrete implementations
public class KingA : King
{
    public override Result Get()
    {
        return new ResultA();
    }
}

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

class Program
{
    static void Main(string[] args)
    {
        IKing ka = new KingA();
        IKing kb = new KingB();

        Result ra = ka.Get();
        Result rb = kb.Get();

        if (ra is ResultA)
        {
            Console.WriteLine("A ok!");
        }

        if (rb is ResultB)
        {
            Console.WriteLine("B ok!");
        }
    }
}

编辑格式)

I think there's some syntax confusion here -- if I captured your intent correctly, this works fine:

// interface to a king
public interface IKing
{
    Result Get();
}

// main abstract class
public abstract class King : IKing
{
    public abstract Result Get();
}

// main abstract result
public abstract class Result
{
    private int _Type;
    public int Type { get { return _Type; } set { _Type = value; } }
}

// KingA result
public class ResultA : Result
{

}

// KingB result
public class ResultB : Result
{

}

// concrete implementations
public class KingA : King
{
    public override Result Get()
    {
        return new ResultA();
    }
}

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

class Program
{
    static void Main(string[] args)
    {
        IKing ka = new KingA();
        IKing kb = new KingB();

        Result ra = ka.Get();
        Result rb = kb.Get();

        if (ra is ResultA)
        {
            Console.WriteLine("A ok!");
        }

        if (rb is ResultB)
        {
            Console.WriteLine("B ok!");
        }
    }
}

(edited for formatting)

何以心动 2024-09-25 18:58:27

您应该能够在 Get 的实现中将 ResultAResultB 显式转换为 Result (我假设这就是“具体实现”的目的)。

You should be able to explicitly cast ResultA and ResultB as Result in the implementations of Get (I'm assuming that's what the "concrete implementations" are intended to be).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文