【问题标题】:how to pass null arguments to base class constructor c#如何将空参数传递给基类构造函数c#
【发布时间】:2018-12-07 15:11:35
【问题描述】:

我有两个扩展基类的类,基类需要 5 个参数,但在我的两个派生类中我不想传递 5 个参数。谁能告诉我如何绕过它。我是新来的。请帮忙

例子:

基类

    public abstract class xyz<TOrderView, Component1, Component2, Component3, Component4>        

派生类 1

    public class abcdef : xyz<IOrderView, Component1, Component2, Component3>

派生类 2 公共类 rdesa : xyz

类 abcdef 不需要 5 个参数,但类 rdesa 需要传递 5 个参数。谁能告诉我如何将 null 作为参数传递给基类。 我是新手,请帮忙。

【问题讨论】:

  • 模板参数是类型,不能为空。我想知道这里是否存在设计问题。一个更具描述性的示例将阐明这一点
  • 这是一篇关于基类和构造函数的有用文章:jonskeet.uk/csharp/constructors.html
  • 你不能那样做。这就是继承的工作原理。此外,出于这个原因,您应该去阅读他们说要使用Composition over Inheritance 的设计模式。这意味着接口将更适合这里。简而言之.. 除非您确切知道如何使用继承.. 否则不要这样做!

标签: c# .net


【解决方案1】:

长答案:

在阅读了 Liskov 替换原则后,您需要重新检查您的继承层次结构。

简短回答: 您可以将一些可接受的类型传递给基类构造函数。

public class abcdef<IOrderView, Component1, Component2, Component3> 
    : xyz<IOrderView, Component1, Component2, Component3, object>

【讨论】:

    猜你喜欢
    • 2014-12-25
    • 2023-04-04
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    相关资源
    最近更新 更多