【发布时间】:2013-10-02 16:22:19
【问题描述】:
如何将通用覆盖“TooLow”getter 代码提取到单个“template”getter?泛型?重载'
get {
bool rtn = _prmpt.MinValue.HasValue && (_prmpt.ResultValue < _prmpt.MinValue);
return rtn;
}
目标是只拥有此代码一次。但是我还没有弄清楚如何处理'int?和“十进制?”使用泛型正确调用 .HasValue 和 <。 . . .建议?提前谢谢你。
/// <summary>
/// abstracted Generic Prompt Base
/// </summary>
public abstract class GenPromptBase
{
public string InputValueType { get; set; }
public abstract bool TooLow { get; }
}
/// <summary>
/// Derived Generic 'Money' class of 'GenPromptBase'
/// </summary>
public class GenPromptMoney : GenPromptBase
{
PromptMoney _prmpt;
public GenPromptMoney(PromptMoney prmptParms)
{
_prmpt = prmptParms;
InputValueType = _prmpt.InputValueType;
}
public override void ParseInput(string result)
{
_prmpt.ResultValue = decimal.Parse(result);
}
public override bool TooLow
{
get
{
bool rtn = _prmpt.MinValue.HasValue && (_prmpt.ResultValue < _prmpt.MinValue);
return rtn;
}
}
}
/// <summary>
/// Derived Generic 'Value' class of 'GenPromptBase'
/// </summary>
public class GenPromptValue : GenPromptBase
{
PromptValue _prmpt;
public GenPromptValue(PromptValue prmptParms)
{
_prmpt = prmptParms;
InputValueType = _prmpt.InputValueType;
}
public override void ParseInput(string result)
{
_prmpt.ResultValue = int.Parse(result);
}
public override bool TooLow
{
get
{ bool rtn = _prmpt.MinValue.HasValue && (_prmpt.ResultValue < _prmpt.MinValue);
return rtn;
}
}
}
/// <summary>
/// Generic Prompt Class
/// </summary>
public class GenPrompt<Z>
{
public string InputValueType { get; set; }
public Z MinValue;
public Z MaxValue;
}
/// <summary>
/// Derived 'Money' class of 'GenPrompt« decimal? »'
/// </summary>
public class PromptMoney : GenPrompt<decimal?>
{
public PromptMoney(
decimal? minValue = null,
decimal? maxValue = null,
string inputValueType = Constants.U_GOI_FORMAT_OPTION_MONEY)
{
InputValueType = inputValueType;
MinValue = minValue;
MaxValue = maxValue;
ResultValue = null;
}
public decimal? ResultValue;
}
/// <summary>
/// Derived 'Value' class of 'GenPrompt« int? »'
/// </summary>
public class PromptValue : GenPrompt<int?>
{
public PromptValue(
int? minValue = null,
int? maxValue = null,
string inputValueType = Constants.U_GOI_FORMAT_OPTION_NUMBER)
{
InputValueType = inputValueType;
MinValue = minValue;
MaxValue = maxValue;
ResultValue = null;
}
public int? ResultValue;
}
【问题讨论】:
标签: c# templates generics overriding overloading