C# 在get调用之前修改值
我有一个依赖于其他属性的属性,代码如下所示:C# 在get调用之前修改值,c#,C#,我有一个依赖于其他属性的属性,代码如下所示: get { if (this.FuelType == "Benzin") { if (this.KmPrL >= 18) { return 'A'; } else if (this.KmPrL &l
get
{
if (this.FuelType == "Benzin")
{
if (this.KmPrL >= 18)
{
return 'A';
}
else if (this.KmPrL < 18 && this.KmPrL >= 14)
{
return 'B';
}
else if (this.KmPrL < 14 && this.KmPrL >= 10)
{
return 'C';
}
else
{
return 'D';
}
}
}
protected virtual GetKmPrL() //Or a more descriptive name
{
return KmPrL
}
...
if (this.FuelType == "Benzin")
{
if (GetKmPrL() >= 18)
{
get
{
如果(this.FuelType==“Benzin”)
{
如果(this.KmPrL>=18)
{
返回“A”;
}
否则如果(this.KmPrL<18&&this.KmPrL>=14)
{
返回“B”;
}
否则如果(this.KmPrL<14&&this.KmPrL>=10)
{
返回“C”;
}
其他的
{
返回“D”;
}
}
}
现在我有另一个类覆盖这个get,我希望它使用这个get调用,但是KmPrL值乘以0.7,但实际上不永久修改属性KmPrL。
我该怎么做呢?我会做一些重构来完成这一点,将您对
this.KmPrL
的使用重构为如下虚拟方法:
get
{
if (this.FuelType == "Benzin")
{
if (this.KmPrL >= 18)
{
return 'A';
}
else if (this.KmPrL < 18 && this.KmPrL >= 14)
{
return 'B';
}
else if (this.KmPrL < 14 && this.KmPrL >= 10)
{
return 'C';
}
else
{
return 'D';
}
}
}
protected virtual GetKmPrL() //Or a more descriptive name
{
return KmPrL
}
...
if (this.FuelType == "Benzin")
{
if (GetKmPrL() >= 18)
{
然后您的派生类可以覆盖它:
protected override GetKmPrL()
{
return KmPrL * .7
}
总的来说,getter属性一开始看起来有点复杂,但通过允许派生类重写依赖属性的计算,您将获得所需的行为。我会进行一些重构来完成这一点,将您对
this.KmPrL
的使用重构为如下虚拟方法:
get
{
if (this.FuelType == "Benzin")
{
if (this.KmPrL >= 18)
{
return 'A';
}
else if (this.KmPrL < 18 && this.KmPrL >= 14)
{
return 'B';
}
else if (this.KmPrL < 14 && this.KmPrL >= 10)
{
return 'C';
}
else
{
return 'D';
}
}
}
protected virtual GetKmPrL() //Or a more descriptive name
{
return KmPrL
}
...
if (this.FuelType == "Benzin")
{
if (GetKmPrL() >= 18)
{
然后您的派生类可以覆盖它:
protected override GetKmPrL()
{
return KmPrL * .7
}
总的来说,getter属性一开始看起来有点复杂,但通过允许派生类重写依赖属性的计算,您将获得所需的行为。我将更改您的属性getter以在检查中使用您的因子。然后在覆盖中,将系数设置为0.7
public virtual char FuelRating
{
get
{
return CalculateRating(this.FuelType, this.KmPrL, 1.0);
}
}
internal char CalculateRating(string fuelType, double kmprl, double factor)
{
double x = kmprl * factor;
if (this.FuelType == "Benzin")
{
if (x >= 18)
{
return 'A';
}
else if (x < 18 && x >= 14)
{
return 'B';
}
else if (x < 14 && x >= 10)
{
return 'C';
}
else
{
return 'D';
}
}
else
return char.MinValue; // not clear what is expected for other fuel types.
}
最后,作为旁白,您应该小心在属性getter中执行复杂的逻辑。属性应该是轻量级的,如果其中有代价高昂的操作,您可能会遇到性能问题。我将更改您的属性getter,以便在检查中使用您的因子。然后在覆盖中,将系数设置为0.7
public virtual char FuelRating
{
get
{
return CalculateRating(this.FuelType, this.KmPrL, 1.0);
}
}
internal char CalculateRating(string fuelType, double kmprl, double factor)
{
double x = kmprl * factor;
if (this.FuelType == "Benzin")
{
if (x >= 18)
{
return 'A';
}
else if (x < 18 && x >= 14)
{
return 'B';
}
else if (x < 14 && x >= 10)
{
return 'C';
}
else
{
return 'D';
}
}
else
return char.MinValue; // not clear what is expected for other fuel types.
}
最后,作为旁白,您应该小心在属性getter中执行复杂的逻辑。属性应该是轻量级的,如果其中有代价高昂的操作,您可能会遇到性能问题。代码是否编译?如果
this.FuelType!=“Benzin”
,没有返回语句。实际代码包含一个else
语句,但它与if
语句一样大,所以我没有包含它。该代码是否编译?如果this.FuelType!=“Benzin”
,没有返回语句。实际代码包含一个else
语句,但它与if
语句一样大,所以我没有包含它。