static void Main(string[] args) { long BeginMemoryUsage, ArrayMemoryUsage, EmptyClassObjectMemoryUsage, BaseClassObjectMemoryUsage, DerivedClassObjectMemoryUsage; int[] localNumber; #region 量測 int[1024] 的耗用記憶體 BeginMemoryUsage = GC.GetTotalMemory(true); localNumber = new int[1024]; ArrayMemoryUsage = GC.GetTotalMemory(true); Console.WriteLine("量測 int[1024] 的耗用記憶體"); Console.WriteLine($"int[1024] 耗用記憶體 : {ArrayMemoryUsage - BeginMemoryUsage} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion #region 量測單純繼承之物件使用記憶體使用情況 BeginMemoryUsage = GC.GetTotalMemory(true); ByInheritance.MyEmptyClass fooMyEmptyClass = new ByInheritance.MyEmptyClass(); EmptyClassObjectMemoryUsage = GC.GetTotalMemory(true); ByInheritance.MyBaseClase fooMyBaseClassObject = new ByInheritance.MyBaseClase(); BaseClassObjectMemoryUsage = GC.GetTotalMemory(true); ByInheritance.MyDerivedClass fooMyDerivedClassObject = new ByInheritance.MyDerivedClass(); DerivedClassObjectMemoryUsage = GC.GetTotalMemory(true); Console.WriteLine("量測單純繼承之物件使用記憶體使用情況"); Console.WriteLine($"產生空類別 之執行個體耗用記憶體 : {EmptyClassObjectMemoryUsage - BeginMemoryUsage} "); Console.WriteLine($"建立基底類別之執行個體耗用記憶體 : {BaseClassObjectMemoryUsage - EmptyClassObjectMemoryUsage} "); Console.WriteLine($"建立衍生類別之執行個體耗用記憶體 : {DerivedClassObjectMemoryUsage - BaseClassObjectMemoryUsage} "); Console.WriteLine($"衍生類別之執行個體 與 基底類別之執行個體 記憶體相差數量 : " + $"{(BaseClassObjectMemoryUsage - EmptyClassObjectMemoryUsage) - (DerivedClassObjectMemoryUsage - BaseClassObjectMemoryUsage)} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion #region 量測繼承與多型覆寫之物件使用記憶體使用情況 BeginMemoryUsage = GC.GetTotalMemory(true); ByInheritancePolymorphismOverride.MyEmptyClass fooInheritancePolymorphismMyEmptyClass = new ByInheritancePolymorphismOverride.MyEmptyClass(); EmptyClassObjectMemoryUsage = GC.GetTotalMemory(true); ByInheritancePolymorphismOverride.MyBaseClase fooInheritancePolymorphismMyBaseClassObject = new ByInheritancePolymorphismOverride.MyBaseClase(); BaseClassObjectMemoryUsage = GC.GetTotalMemory(true); ByInheritancePolymorphismOverride.MyDerivedClass fooInheritancePolymorphismMyDerivedClassObject = new ByInheritancePolymorphismOverride.MyDerivedClass(); DerivedClassObjectMemoryUsage = GC.GetTotalMemory(true); var foo = (ByInheritancePolymorphismOverride.MyBaseClase)fooInheritancePolymorphismMyDerivedClassObject; Console.WriteLine("量測繼承與多型覆寫之物件使用記憶體使用情況"); Console.WriteLine($"產生空類別 之執行個體耗用記憶體 : {EmptyClassObjectMemoryUsage - BeginMemoryUsage} "); Console.WriteLine($"建立基底類別之執行個體耗用記憶體 : {BaseClassObjectMemoryUsage - EmptyClassObjectMemoryUsage} "); Console.WriteLine($"建立衍生類別之執行個體耗用記憶體 : {DerivedClassObjectMemoryUsage - BaseClassObjectMemoryUsage} "); Console.WriteLine($"衍生類別之執行個體 與 基底類別之執行個體 記憶體相差數量 : " + $"{(DerivedClassObjectMemoryUsage - BaseClassObjectMemoryUsage)- (BaseClassObjectMemoryUsage - EmptyClassObjectMemoryUsage)} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion #region 量測繼承與多型隱藏之物件使用記憶體使用情況 BeginMemoryUsage = GC.GetTotalMemory(true); ByInheritancePolymorphismNew.MyEmptyClass fooInheritancePolymorphismNewMyEmptyClass = new ByInheritancePolymorphismNew.MyEmptyClass(); EmptyClassObjectMemoryUsage = GC.GetTotalMemory(true); ByInheritancePolymorphismNew.MyBaseClase fooInheritancePolymorphismNewMyBaseClassObject = new ByInheritancePolymorphismNew.MyBaseClase(); BaseClassObjectMemoryUsage = GC.GetTotalMemory(true); ByInheritancePolymorphismNew.MyDerivedClass fooInheritancePolymorphismNewMyDerivedClassObject = new ByInheritancePolymorphismNew.MyDerivedClass(); DerivedClassObjectMemoryUsage = GC.GetTotalMemory(true); var foo1 = (ByInheritancePolymorphismOverride.MyBaseClase)fooInheritancePolymorphismMyDerivedClassObject; Console.WriteLine("量測繼承與多型隱藏之物件使用記憶體使用情況"); Console.WriteLine($"產生空類別 之執行個體耗用記憶體 : {EmptyClassObjectMemoryUsage - BeginMemoryUsage} "); Console.WriteLine($"建立基底類別之執行個體耗用記憶體 : {BaseClassObjectMemoryUsage - EmptyClassObjectMemoryUsage} "); Console.WriteLine($"建立衍生類別之執行個體耗用記憶體 : {DerivedClassObjectMemoryUsage - BaseClassObjectMemoryUsage} "); Console.WriteLine($"衍生類別之執行個體 與 基底類別之執行個體 記憶體相差數量 : " + $"{(DerivedClassObjectMemoryUsage - BaseClassObjectMemoryUsage)- (BaseClassObjectMemoryUsage - EmptyClassObjectMemoryUsage)} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion }
static void Main(string[] args) { #region 單純繼承之物件多型行為 ByInheritance.MyDerivedClass fooMyDerivedClassObject = new ByInheritance.MyDerivedClass(); ByInheritance.MyBaseClase fooMyBaseClassObject = fooMyDerivedClassObject as ByInheritance.MyBaseClase; Console.WriteLine("單純繼承之物件多型行為"); Console.WriteLine($"型別為基底類別的本地變數之執行時期的型別為 : {fooMyBaseClassObject.GetType().Name} "); Console.WriteLine($"型別為衍生類別的本地變數之執行時期的型別為 : {fooMyDerivedClassObject.GetType().Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Name 屬性值為 : {fooMyBaseClassObject.Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Address 屬性值為 : {fooMyBaseClassObject.Address} "); Console.WriteLine($"型別為衍生類別的本地變數之 Name 屬性值為 : {fooMyDerivedClassObject.Name} "); Console.WriteLine($"型別為衍生類別的本地變數之 Address 屬性值為 : {fooMyDerivedClassObject.Address} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion #region 繼承與多型覆寫之有使用 virtual / override 使用情況 ByInheritancePolymorphismVirtualOverride.MyDerivedClass fooVirtualOverrideMyDerivedClassObject = new ByInheritancePolymorphismVirtualOverride.MyDerivedClass(); ByInheritancePolymorphismVirtualOverride.MyBaseClase fooVirtualOverrideMyBaseClassObject = fooVirtualOverrideMyDerivedClassObject as ByInheritancePolymorphismVirtualOverride.MyBaseClase; Console.WriteLine("繼承與多型覆寫之有使用 virtual / override 使用情況"); Console.WriteLine($"型別為基底類別的本地變數之執行時期的型別為 : {fooVirtualOverrideMyBaseClassObject.GetType().Name} "); Console.WriteLine($"型別為衍生類別的本地變數之執行時期的型別為 : {fooVirtualOverrideMyDerivedClassObject.GetType().Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Name 屬性值為 : {fooVirtualOverrideMyBaseClassObject.Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Address 屬性值為 : {fooVirtualOverrideMyBaseClassObject.Address} "); Console.WriteLine($"型別為衍生類別的本地變數之 Name 屬性值為 : {fooVirtualOverrideMyDerivedClassObject.Name} "); Console.WriteLine($"型別為衍生類別的本地變數之 Address 屬性值為 : {fooVirtualOverrideMyDerivedClassObject.Address} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion #region 繼承與多型覆寫之有使用 new 使用情況 ByInheritancePolymorphismNew.MyDerivedClass fooNewMyDerivedClassObject = new ByInheritancePolymorphismNew.MyDerivedClass(); ByInheritancePolymorphismNew.MyBaseClase fooNewMyBaseClassObject = fooNewMyDerivedClassObject as ByInheritancePolymorphismNew.MyBaseClase; Console.WriteLine("繼承與多型覆寫之有使用 new 使用情況"); Console.WriteLine($"型別為基底類別的本地變數之執行時期的型別為 : {fooNewMyBaseClassObject.GetType().Name} "); Console.WriteLine($"型別為衍生類別的本地變數之執行時期的型別為 : {fooNewMyDerivedClassObject.GetType().Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Name 屬性值為 : {fooNewMyBaseClassObject.Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Address 屬性值為 : {fooNewMyBaseClassObject.Address} "); Console.WriteLine($"型別為衍生類別的本地變數之 Name 屬性值為 : {fooNewMyDerivedClassObject.Name} "); Console.WriteLine($"型別為衍生類別的本地變數之 Address 屬性值為 : {fooNewMyDerivedClassObject.Address} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion #region 繼承與多型覆寫之有使用 virtual / override / new 使用情況 ByInheritancePolymorphismMixVirtualAndNew.MyDerivedClass fooMixVirtualAndNewMyDerivedClassObject = new ByInheritancePolymorphismMixVirtualAndNew.MyDerivedClass(); ByInheritancePolymorphismMixVirtualAndNew.MyBaseClase fooMixVirtualAndNewMyBaseClassObject = fooMixVirtualAndNewMyDerivedClassObject as ByInheritancePolymorphismMixVirtualAndNew.MyBaseClase; Console.WriteLine("承與多型覆寫之有使用 virtual / override / new 使用情況"); Console.WriteLine($"型別為基底類別的本地變數之執行時期的型別為 : {fooMixVirtualAndNewMyBaseClassObject.GetType().Name} "); Console.WriteLine($"型別為衍生類別的本地變數之執行時期的型別為 : {fooMixVirtualAndNewMyDerivedClassObject.GetType().Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Name 屬性值為 : {fooMixVirtualAndNewMyBaseClassObject.Name} "); Console.WriteLine($"型別為基底類別的本地變數之 Address 屬性值為 : {fooMixVirtualAndNewMyBaseClassObject.Address} "); Console.WriteLine($"型別為基底類別的本地變數之 Age 屬性值為 : {fooMixVirtualAndNewMyBaseClassObject.Age} "); Console.WriteLine($"型別為衍生類別的本地變數之 Name 屬性值為 : {fooMixVirtualAndNewMyDerivedClassObject.Name} "); Console.WriteLine($"型別為衍生類別的本地變數之 Address 屬性值為 : {fooMixVirtualAndNewMyDerivedClassObject.Address} "); Console.WriteLine($"型別為衍生類別的本地變數之 Age 屬性值為 : {fooMixVirtualAndNewMyDerivedClassObject.Age} "); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); #endregion }