本文介绍了从另一个调用一个构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个构造它助长值只读字段。
I have two constructors which feed values to readonly fields.
class Sample
{
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
_intField = i;
}
public Sample(int theInt)
{
_intField = theInt;
}
public int IntProperty
{
get { return _intField; }
}
private readonly int _intField;
}
一个构造直接接收的值,并且其它的确一些计算,并且获得的值,然后设置字段
One constructor receives the values directly, and the other does some calculation and obtains the values, then sets the fields.
现在这里的渔获:
- 我不想重复
设置code。在这种情况下,只有一个
字段设置,但当然也有可能
以及一个以上的 - 要使字段只读的,我需要
从构造函数中进行设置,这样
我不能提取共享code到
效用函数。 - 我不知道该怎么CALL ONE
从另一个构造。
任何想法?
推荐答案
这样的:
public Sample(string str) : this(int.Parse(str)) {
}
这篇关于从另一个调用一个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!