派生类作为返回值

发布于 2024-09-13 07:34:03 字数 594 浏览 8 评论 0原文

给定这些 C# 类(由 WCF 生成,我无法更改这些):

public SysState GetSysState();

public class SysState { /* nothing much here */}
public class Normal : SysState { /* properties & methods */  }
public class Foobar : SysState { /* different properties & methods */  }

我的代码(当前):

SysState result = GetSysState();

if (result is Normal) HandleNormal((Normal) result);

if (result is Foobar) HandleFoobar((Foobar) result);

我的问题: 我一直觉得我错过了一些明显的东西,我不需要明确地检查类型。我有高级时刻吗?

Given these C# classes (generated by WCF, I can't change these):

public SysState GetSysState();

public class SysState { /* nothing much here */}
public class Normal : SysState { /* properties & methods */  }
public class Foobar : SysState { /* different properties & methods */  }

My code (currently):

SysState result = GetSysState();

if (result is Normal) HandleNormal((Normal) result);

if (result is Foobar) HandleFoobar((Foobar) result);

My question: I keep feeling I'm missing something obvious, that I shouldn't need to check type explicitly. Am I having a senior moment?

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

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

发布评论

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

评论(2

山川志 2024-09-20 07:34:03

使用虚拟方法。将您的代码放在它们所操作的类中,而不是放在获取对该类的引用的某些代码中。

public class SysState {
  /* nothing much here, except...: */
  public abstract virtual void DoSomething();
}

public class Normal : SysState {
  /* properties & methods */
  public override void DoSomething()
  {
    // ...
  }
}

public class Foobar : SysState {
  /* different properties & methods */
  public override void DoSomething()
  {
    // ...
  }
}

SysState result = SomeFunctionThatReturnsObjectDerivedFromSysState();

result.DoSomething();

这将执行派生类的 DoSomething 方法。这称为多态性,是继承最自然的(有些人认为这是唯一正确)的使用。

请注意,SysState.DoSomething 不必是抽象的才能工作,但它必须是虚拟的。

Use a virtual method. Put your code in the classes that they operate on, not in some code that gets a reference to the class.

public class SysState {
  /* nothing much here, except...: */
  public abstract virtual void DoSomething();
}

public class Normal : SysState {
  /* properties & methods */
  public override void DoSomething()
  {
    // ...
  }
}

public class Foobar : SysState {
  /* different properties & methods */
  public override void DoSomething()
  {
    // ...
  }
}

SysState result = SomeFunctionThatReturnsObjectDerivedFromSysState();

result.DoSomething();

This will execute the derived class's DoSomething method. This is called polymorphism, and is the most natural (and some would argue the only correct) use of inheritance.

Please note that SysState.DoSomething doesn't have to be abstract for this to work, but it does have to be virtual.

一口甜 2024-09-20 07:34:03

您可以尝试组合 handleX 并将 Handle 放入 SysState 中,并在 Normal中覆盖它Foobar 执行特定任务。它可能不是完美的解决方案,但看起来相对简洁。如果需要从其他来源输入数据,将它们作为参数传递?

public class SysState
{
    public bool Process(Information info)
    {
        return ( info.Good );
    }
}

public class Normal
{
    public bool Process(Information info)
    {
        return doStuff();
    }
}

public class Foobar
{
    public bool Process(Information info)
    {
        return diePainfully();
    }
}

显然只是一个例子,不知道 HandleNormal 和 HandleFoobar 是做什么的,但它可以很好地工作。

You might try combining handleX and placing Handle in SysState and over-riding it in both Normal and Foobar to perform specific tasks. It may not be the perfect solution, but it would look relatively neat. If you need to input data from other sources, pass them as parameters?

public class SysState
{
    public bool Process(Information info)
    {
        return ( info.Good );
    }
}

public class Normal
{
    public bool Process(Information info)
    {
        return doStuff();
    }
}

public class Foobar
{
    public bool Process(Information info)
    {
        return diePainfully();
    }
}

Obviously just an example, not knowing what HandleNormal and HandleFoobar do, but it could work nicely.

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