0
  • 基本的に製品の価格と整数の合計を DB にチェックする RoundedSum オブジェクトがあります。
  • 基本的に RoundedSum を継承し、checkinsum 関数をオーバーライドし、最も近い整数との差を unaccounted にチェックする UnRoundedSum オブジェクトがあります。
  • UnRoundedSum は base の checkinsum(productid,int) を呼び出して価格値を DB にチェックします これは 2 つのクラスを持つ私の名前空間 SumRounding です

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);
        }
    }
}

これは、私のプロジェクトで動作していないため、儀式をテストするために作成したメイン関数を想定しています。これは、上記のクラスのテストオブジェクトのようなものです。

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);        
            }
        }
    }

スタック オーバーフローの例外イメージ


デバッグしようとすると、StackOverflow エラーがスローされる前に常に checkinsum() でキャッチされます。デバッグしようとすると、実行後でも checkinsum() に戻ります。なぜか戻ってきます。何がそんなにうまくいかないのかわからない。

4

3 に答える 3

4
    checkinsum(productid,intsum);

する必要があります

    base.checkinsum(productid,intsum);

UnRoundedSum クラス内

編集:説明、ベースなし。(これはベースクラスに移動し、そこでメソッドを呼び出します)UnRoundedSumで自分自身を呼び出すため、スタックオーバーフローを引き起こす無限ループになります

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 を実行します。

于 2013-01-31T18:35:26.837 に答える
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);
}
于 2013-01-31T18:34:26.310 に答える
1

C# 標準では、「派生クラスのメソッドが適用可能な場合、基本クラスのメソッドは候補にならない」と規定されています。つまり、前者は派生クラスにあり、C# では anを a に暗黙的にキャストできるため、呼び出し時にcheckinsum(int,float)は常に優先されます。base.checkinsum(int,int)checkinsum(1,1)intfloat

参照: http://blogs.msdn.com/b/ericlippert/archive/2007/09/04/future-breaking-changes-part-three.aspx

于 2013-01-31T18:54:46.487 に答える