基类变量的值如何从派生类的变量中延迟
本文关键字:变量 派生 延迟 类变量 基类 | 更新日期: 2023-09-27 18:18:11
在C#程序中,基类的值低于派生类的源,该变量是继承的,基类是抽象的,但变量仅在基类中声明。这是 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
让我们逐行执行代码,看看每个对象的iCount
字段发生了什么:
CDerivedClass deriveObject = new CDerivedClass();
CBaseClass basePointer = new CDerivedClass();
CDerivedClass notUsed;
// deriveObject.iCount == 50
// basePointer.iCount == 50
deriveObject
和 basePointer
都iCount
初始化为 50,因为构造函数在 C# 中对派生对象进行调用的方式。
在这种情况下:
- 调用基类构造函数,首先将"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();
deriveObject
和 basePointer
引用了两个不同的CDerivedClass
实例。
唯一棘手的部分是对basePointer.Subtract()
的调用("3"(。由于CDerivedClass
不会将其Subtract
方法标记为"重写",因此调用基类的方法。