基类变量的值如何从派生类的变量推迟?

时间:2014-08-16 18:27:13

标签: c#

在c#program下面的baseclass值从派生类中继承了相同的继承变量,baseclass是abstract,但变量只在baseclass中声明。 这是C#控制台程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestApplication
{
    class MainClass
    {
        abstract class CBaseClass
        {
            public int iCount;

            public CBaseClass()
            {
                Console.WriteLine("CBaseClass created");
                iCount = 30;
            }

            public virtual void Add() { iCount += 20; }
            public virtual void Add(int iAdd) { iCount += iAdd; }
            public void Subtract() { iCount -= 20; }
            public abstract void Subtract(int iSubtract);
            public override string ToString()
            {
                return base.ToString() + ".. Method of BaseClass";
            }
            ~CBaseClass() { Console.WriteLine("CBaseClass deleted"); }
        };

        class CDerivedClass : CBaseClass 
        {
            public CDerivedClass() 
            { 
                Console.WriteLine(" CDerivedClass created.");
                iCount = 50;
            }

            public override void Add()
            {
                iCount += 20;
            }

            public void Add(int iAdd)
            { 

            iCount  += iAdd;
            }

            public void Subtract() 
            {
                iCount -= 40;
            }

            public sealed override void Subtract(int iSubtract)
            {
                iCount -= 10;
            }
            ~CDerivedClass() { Console.WriteLine("CDerivedClass deleted \n"); }

      };


        static void Main(string[] args)
        {
            CDerivedClass deriveObject = new CDerivedClass();
            CBaseClass basePointer = new CDerivedClass();
            CDerivedClass notUsed;

            Console.WriteLine("1:" + deriveObject.iCount);
            deriveObject.Add();
            Console.WriteLine("2:" + deriveObject.iCount);
            basePointer.Add(30);
            Console.WriteLine("3:" + basePointer.iCount);
            basePointer.Subtract();
            Console.WriteLine("4:" + basePointer.iCount);
            basePointer.Subtract(20);
            Console.WriteLine("5:" + basePointer.iCount);
            Console.WriteLine("6:{0}",basePointer);
            Console.ReadLine();

        }
    }
}

这是输出:

CBaseClass created
CDerivedClass created.

CBaseClass created
CDerivedClass created.
1:50
2:70
3:80
4:60
5:50
6:TestApplication.MainClass+CDerivedClass..Method of BaseClass

在上面的输出中,当我们调用基类方法add(30)时,我无法理解..

basePointer.Add(30);当我们调用此方法时......在iCount的值为70之前,但在此之后它变为50.如何? 我期望输出3:100

1 个答案:

答案 0 :(得分:2)

让我们逐行浏览代码,看看每个对象的iCount字段发生了什么:

CDerivedClass deriveObject = new CDerivedClass();
CBaseClass basePointer = new CDerivedClass();
CDerivedClass notUsed;

// deriveObject.iCount == 50
// basePointer.iCount == 50
由于the way constructors are called on derived objects in C#

deriveObjectbasePointer都将iCount初始化为50。

在这种情况下:

  • 调用基类构造函数,首先将`iCount`值设置为30并打印基类消息。
  • 接下来,调用子类构造函数,将`iCount`设置为50(并覆盖30)。
Console.WriteLine("1:" + deriveObject.iCount); // 1:50
deriveObject.Add();
// CDerivedClass.Add is called on deriveObject
// deriveObject.iCount == 70

Console.WriteLine("2:" + deriveObject.iCount); // 2:70
basePointer.Add(30);
// CDerivedClass.Add is called on basePointer
// basePointer.iCount == 80

Console.WriteLine("3:" + basePointer.iCount); // 3:80
basePointer.Subtract();
// CBaseClass.Subtract is called on basePointer, since `CDerivedClass` does not
// override the Subtract method.
// basePointer.iCount == 60

Console.WriteLine("4:" + basePointer.iCount); // 4:60
basePointer.Subtract(20);
// CDerivedClass.Subtract is called on basePointer. *Note* that this subtracts 10.
// basePointer.iCount == 50

Console.WriteLine("5:" + basePointer.iCount); // 5:50
Console.WriteLine("6:{0}",basePointer);

Console.ReadLine();

deriveObjectbasePointer引用CDerivedClass的两个不同实例。

唯一棘手的部分是调用basePointer.Subtract()(" 3")。由于CDerivedClass未将其Subtract方法标记为"覆盖",因此基类'方法被称为。