构造函数应该有多少变数呢? [英] How many variables should a constructor have?

查看:134
本文介绍了构造函数应该有多少变数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这是一个非常开放的问题,可以得到多种答案,但在这里不用。

I realize this is a pretty open question and could get a variety of answers, but here goes.

使用C#(或Java或任何面向对象的语言),有没有指出有多少变量应该传递到构造一般规则?变量我传递到扩展类的构造函数的数量似乎失控。

Using C# (or Java, or any OO language), is there a general rule that states how many variables should be passed into the constructor? The number of variables I am passing into the constructor of the extended classes seem to be getting out of hand.

在努力封装类的数据,我宣布成员私有的,初始化它们在我的构造函数,使用公共访问。

In an effort to encapsulate the data of a class, I declare the members private, initialize them in my constructor, and use public accessors.

下面是一个例子:

public class A
{
  private int var1;
  private int var2;
  private int var3;

  //3 variables passed in
  public A(int v1, int v2, int v3)
  {
    var1 = v1;
    var2 = v2;
    var3 = v3;
  }

  //Properties (accessors) here
}

public class B : A
{
  private int var4;
  private int var5;

  //5 variables passed in
  public B(int v1, int v2, int v3, int v4, int v5)
  : base(v1,v2,v3)
  {
    var4 = v4;
    var5 = v5;
  }

  //Properties (accessors) here
}

public class C : B
{
  private int var6;
  private int var7;

  //7 variables passed in !!!
  public C(int v1, int v2, int v3, int v4, int v5, int v6, int v7)
  : base(v1,v2,v3,v4,v5)
  {
    var6 = v6;
    var7 = v7;
  }

  //Properties (accessors) here
}

我的构造函数通常传递不同的对象,不只是整型。我开始质疑我的设计,当我开始在7个变量传递到子类的构造函数,但我也有麻烦找出一种不同的方式来做到这一点。

My constructors are usually passing in different objects, not just ints. I started questioning my design when I started passing in 7 variables to the constructor of the child class, but I have also had trouble figuring out a different way to do this.

这被认为是不好的编程习惯?是否有一个总体限制,你应该传递到构造函数的变量的数目?

Is this considered bad programming practice? Is there a general limit to the number of variables you should pass into a constructor?

推荐答案

一般来说,我发现,如果有更多的大于3,这是一个标志,以做到对设计的快速完整性检查。如果有超过5个,这是一个重大的警告的东西可能是错误的设计

Generally I've found if there's more than 3, that's a sign to do a quick sanity check on the design. If there's more than 5, that's a major warning that something is probably wrong with the design.

不过,请注意这个词可能 - 最终,唯一真正的规则的根据需要的功能,不能多也不能少使用尽可能多的。总有一些例外并在多个参数是最有意义的案例。

However, note the word "probably" - in the end, the only real rule is use as many as needed to function, no more and no less. There are always exceptions and cases where more parameters makes the most sense.

如果该参数是某种联系,你应该把它们封装到一个容器类。

If the parameters are related somehow, you should encapsulate them into a container class.

如果该参数是不相关的 - 例如它没有任何意义将它们分组到一个容器类 - 类是可能做的东西太多了。一般没有理由一个类应该知道的7完全不同的信息片段。掰开类成单独的类。它可能是有意义的委托参数3和4的子类和5,6和7到另一个类,例如 - 和你的父类简单地协调它们之间的操作。

If the parameters are not related - e.g. it makes no sense to group them into a container class - your class is probably doing too many things. There's generally no reason a single class should be aware of 7 totally disparate pieces of information. Break your class apart into separate classes. It might make sense to delegate parameters 3 and 4 to a subclass and 5, 6 and 7 to another class for example - and your parent class simply coordinates the operations between them.

这篇关于构造函数应该有多少变数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆