如何正确的stackoverflow异常在“获取”部分代码 [英] how do correct stackoverflow Exception at the "get" part of the code
本文介绍了如何正确的stackoverflow异常在“获取”部分代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class appliance
{
double soln1,soln2,soln3...soln20;
public double Total {
get {
return Total;
}
set {
Total=soln1+soln2+soln3......soln20;
}
}
}
推荐答案
看起来你有代码中的递归。
It looks like you're having a recursion in your code. The
return Total;
其实再次调用Total getter,再次调用它等等。
为了存储值,你应该有一个后备变量,你可以在课堂内部使用它。所以类似于:
Actually calls the Total getter again and this calls it again and so on.
In order to store the value you should have a backing variable which you would use internally in the class. So something like:
class appliance
{
double soln1,soln2,soln3...soln20;
private double _total;
public double Total {
get {
return this._total;
}
set {
this._total=soln1+soln2+soln3......soln20;
}
}
但是,由于您根据传递给属性的其他变量设置值,因此设置器看起来有点奇怪。重新检查代码是否符合要求或是否应该使用
However, the setter looks a bit odd since you set the value based on other variables than what is passed to the property. Re-check that the code is as required or should you use
set {
this._total=value;
}
这篇关于如何正确的stackoverflow异常在“获取”部分代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文