namespace DatabasePricing.SumRounding
{
public class Roundedsum
{
public void checkinsum(int productid,int sum)
{
//Checks in price in the price table
//dbobject("price",productid,sum);
int temp_int = sum;
}
}
public class UnRoundedSum : Roundedsum
{
public void checkinsum(int productid,float sum)
{
//Since the sum is a float it will check the difference
//into unroundedsum table in the database
int intsum = (int)sum;
float tempfloat = sum - intsum;
//Check this remaining float into the database under unaccounted
// dbobject("unroundedsum",productid,tempfloat);
//Now call the integer checksum with the integer value
checkinsum(productid,intsum);
}
}
}
这是让我们假设我现在为测试rite创建的一个主要函数,因为它在我的项目中不起作用。这就像上面类的测试对象。</ strong>
using DatabasePricing.SumRounding;
namespace DatabasePricing
{
class testingrounding
{
static void Main() {
int product_id = 1;
float float_value = 1.1f;
UnRoundedSum obj1 = new UnRoundedSum();
//This call produces StackOverflow Exception
obj1.checkinsum(1, float_value);
int price = 200;
//I tried with integer value to test RoundedSum object
//it is still throwing an exception
//This call also produces StackOverflow Exception
obj1.checkinsum(1, price);
}
}
}
当我尝试调试时,它始终在checkinsum()中被捕获,然后才会抛出StackOverflow错误。当我尝试调试时,即使执行它也会返回到checkinsum()。它因某种原因不断回归。我不知道会出现什么问题。
答案 0 :(得分:4)
checkinsum(productid,intsum);
应该是
base.checkinsum(productid,intsum);
在UnRoundedSum类
中编辑:Explination,没有基础。 (这是去基类,然后在那里调用方法)它会在UnRoundedSum中调用自己,所以它将是一个无限循环,这将导致stackoverflow
EDIT2:
阅读你的评论后,我想你想要这个:
public class sum
{
public void checkinsum(int productid, float sum)
{
//Since the sum is a float it will check the difference
//into unroundedsum table in the database
int intsum = (int)sum;
float tempfloat = sum - intsum;
//Check this remaining float into the database under unaccounted
// dbobject("unroundedsum",productid,tempfloat);
//Now call the integer checksum with the integer value
}
public void checkinsum(int productid, int sum)
{
//Checks in price in the price table
//dbobject("price",productid,sum);
int temp_int = sum;
}
}
然后它将执行您想要的方法,或者它是int int或int float。
答案 1 :(得分:1)
您在checkinsum
中进行了无限递归调用。
您可以在base.checkinsum
UnRoundedSum.checkinsum
public void checkinsum(int productid,float sum)
{
//Since the sum is a float it will check the difference
//into unroundedsum table in the database
int intsum = (int)sum;
float tempfloat = sum - intsum;
//Check this remaining float into the database under unaccounted
// dbobject("unroundedsum",productid,tempfloat);
//Now call the integer checksum with the integer value
base.checkinsum(productid,intsum);
}
答案 2 :(得分:1)
C#标准规定“如果派生类中的任何方法适用,则基类中的方法不是候选者”。换句话说,checkinsum(int,float)
在调用base.checkinsum(int,int)
时始终优先于checkinsum(1,1)
,因为前者位于派生类中,C#允许int
隐式强制转换为{ {1}}。
请参阅:http://blogs.msdn.com/b/ericlippert/archive/2007/09/04/future-breaking-changes-part-three.aspx