简体   繁体   English

静态构造方法与构造函数

[英]static construction method vs constructor

In the .NET framework I often see the idiom where a constructor is replaced by a static function to construct a new object from. 在.NET框架中,我经常看到一个习惯用法,其中构造函数被静态函数替换以构造新对象。

For instance with BigInteger there's no constructor taking a string so this is not possible: 例如,对于BigInteger,没有构造函数采用字符串,因此这是不可能的:

BigInteger i = new BigInteger("1000000103453543897");

But there is a static Parse function. 但是有一个静态Parse函数。

BigInteger i = BigInteger.Parse("1000000103453543897");

Why is a class design like this often chosen? 为什么经常选择这样的课堂设计?

The only thing I can think of is there's one object less to be created and later thrown away. 我唯一能想到的是,有一个对象少被创建,后来被扔掉了。 Is it true that that is the main reason? 这是主要原因吗? Or are there other reasons? 还是有其他原因吗?

BigInteger(string value)
{
  BigInteger result = new BigInteger(); // this one just returned in a Parse function

  // compute bigint

  // copy result to this
  data = new uint[maxLength];
  for (int i = 0; i < result.Length; i++)
    data[i] = result.data[i];

  Length = result.dataLength;  
}

There could be many reasons - research the Factory method pattern . 可能有很多原因 - 研究Factory方法模式

With your example - many consider it a bad practice to have significant logic in/called from a constructor (I don't want to throw an exception from a constructor unless it's a missing parameter). 在您的示例中 - 许多人认为从构造函数中获取/调用重要逻辑是一种不好的做法(我不想从构造函数中抛出异常,除非它是缺少的参数)。 Using a factory method allows for implementation guaranteed to run at object construction but not in the constructor. 使用工厂方法允许实现保证在对象构造中运行但不在构造函数中运行。

there's no constructor taking a string 没有构造函数接受字符串

There's no technical reason why you couldn't. 你不能没有技术上的原因。 The designers just chose not to duplicate code that already exists and adds no pragmatic value - it's still one line of code, it's clearer what you're trying to do, reduces the number of errors by passing in a wrong type, etc. etc. 设计人员只是选择不复制已经存在的代码并且不添加实用价值 - 它仍然是一行代码,它更清楚你要做的事情,通过传入错误的类型减少错误的数量等。

It also makes it consistent with other numeric types that do not have a non-default constructor (you can't say int i = new int(4) ); 它还使它与没有非默认构造函数的其他数字类型一致(你不能说int i = new int(4) );

The bottom line is - the value in such a constructor needs to outweigh the cost to implement, test, document, and ship the new feature. 底线是 - 这种构造函数中的需要超过实现,测试,记录和发布新功能的成本

See @Moho for answer, Factory method pattern. 请参阅@Moho获取答案,工厂方法模式。

I personally think BigInteger.Parse covers it better. 我个人认为BigInteger.Parse更好地覆盖它。 You have some value in this case a string, and you want it to convert it to a BigNumber. 在这种情况下,您有一些值是字符串,并且您希望它将其转换为BigNumber。 A constructor with a string parameter doesn't not tell wat is going to happen. 具有字符串参数的构造函数不会告诉wat将要发生。 For all you know it is just doing a Console.WriteLine with the string instead of something different. 对于你所知道的只是用一个字符串代替不同的东西来做一个Console.WriteLine。 Parse tells you something more... Parse告诉你更多......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM