开发者

why final output is zero : OOPS sample

I have following code

using System;

namespace xyzApp
{
    class Program
    {
        public static void Main(string[] args)
        {               
            Test1Class t = new Test1Class ();
            t.Add(4);
            t.Add(11.1);
            t.showValue();
            Console.Write("Press an开发者_JS百科y key to continue . . . ");
            Console.ReadKey(true);
        }
    }


    class TestClass{
        protected  int sum =0;

        public void Add(int x)
        {
            sum+=x;
        }

        public void showValue()
        {
            Console.WriteLine(" the sum is : {0}",sum);
        }
    }

    class Test1Class :TestClass
    {
        double sum ;

        public void Add(double x)
        {
            sum+=x;
            Console.WriteLine(" the sum is : {0}",sum);
        }
    }
}

The output is

 the sum is : 4
 the sum is : 15.1
 the sum is : 0
Press any key to continue . . .

Can somebody explain, why final output is 0, and how I can get final output as 15.1 without creating method printValue in derived class.

I also like to know how it is different from language to language. Thanks


The sum variable in Test1Class shadows/hides the sum variable in TestClass. Therefore, when you reference sum in Test1Class in the Add method, it refers to Test1Class's variable. In the final print statement in t.showValue(), however, you're calling TestClass's sum variable, which has never yet been changed. This, therefore, gives you the default value of 0.

What you probably meant to do is get rid of Test1Class's member variable altogether and use TestClass's, since you set it to protected anyway so it's readily available to all derived classes.

You probably intended something like this:

  using System;

    namespace xyzApp
    {
        class Program
        {
            public static void Main(string[] args)
            {               
                Test1Class t = new Test1Class ();
                t.Add(4);
                t.Add(11.1);
                t.showValue();
                Console.Write("Press any key to continue . . . ");
                Console.ReadKey(true);
            }
        }


        class TestClass{
            protected  double sum =0;

            public void Add(int x)
            {
                sum+=x;
            }

            public void showValue()
            {
                Console.WriteLine(" the sum is : {0}",sum);
            }
        }

        class Test1Class :TestClass
        {                   
            public void Add(double x)
            {
                sum+=x;
                Console.WriteLine(" the sum is : {0}",sum);
            }
        }
    }

All I did was delete double sum; from the Test1Class and change the sum variable in TestClass to a double, and you should get the results you're looking for. (Using protected variables is not so highly recommmended, though...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜