using System; using System.Collections.Generic; using System.Text; namespace BaseFunc { class Program { static void Main(string[] args) { Test test = new Test(); test.func(1); } } abstract class Base { private string str="Private Value"; public Base() { Console.WriteLine("Lives"); } public void func() { Console.WriteLine("----------->Father Func" +str); } ~Base() { Console.WriteLine("------------->Destroyed."); } } class Test:Base { public void func(int i) { base.func(); Console.WriteLine("----------->Child Func"); } } }
如上代码,一个抽象基类,一个派生类,派生类覆写基类方法,并在该方法中调用基类被覆写方法。
结果如下:
Lives
----------->Father FuncPrivate Value
----------->Child Func
------------->Destroyed.
请按任意键继续. . .
基类的构造函数和析构函数被继承,之前在一本书上看到说析构函数不能被继承,这个析构函数是父类的无疑。
并且,基类是一个抽象类,但类中方法给出了实现,也从基类中成功调用了基类此方法 ,并且访问了抽象类的私有成员,这样的话,系统也给抽象类分配了内存空间了?
浙公网安备 33010602011771号