基礎知識一:

using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication2 { public class ParentClass { public ParentClass() { } public string NamePropety { get; set; } public string GetName() { return ""; } } public class ChildClass : ParentClass { public ChildClass() { } public int Age { get; set; } public int GetAge() { return 10; } } static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { //=>1、實例化父類 ParentClass parent = new ParentClass(); string _NamePropety = parent.NamePropety; string _name = parent.GetName(); //1.1向上轉型 子類轉父類 ParentClass parent1 = new ChildClass(); //或者ParentClass parent1 = new ChildClass() as ParentClass; string _NamePropety1 = parent1.NamePropety; string _name1 = parent1.GetName(); //=>2、實例化子類 ChildClass child = new ChildClass(); string _NamePropety2 = child.NamePropety; string _name2 = child.GetName(); int ageName2 = child.GetAge(); int age2 = child.Age; //2.1向下轉型 父類轉換子類。 ParentClass child3 = new ChildClass(); ChildClass child4 = (ChildClass)child3; string _NamePropety3 = child4.NamePropety; string _name3 = child4.GetName(); int ageName3 = child4.GetAge(); int age3 = child4.Age; //=>3、不正確的父類轉子類。 //as方式轉換。(as 轉換失敗時,程序不會拋異常,child1對象為NULL。) ChildClass child1 = new ParentClass() as ChildClass; //或者 ChildClass child1 = (ChildClass)new ParentClass(); Console.WriteLine(child1.NamePropety); //強制轉換。(程序會拋出異常。) ChildClass child1_1 = (ChildClass)new ParentClass(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
基礎知識二:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using IBO.XJMYQP.ControlLib; namespace IBO.XJMYQP.UI { public class ParentClass { public ParentClass() { Console.WriteLine("初始化父類構造函數"); } public virtual void Test1() { Console.WriteLine("我是基類的Test1"); } public void Test2() { Console.WriteLine("我是基類的Test2"); } public virtual void Test3() { Console.WriteLine("我是基類的Test3"); } //=>//protected訪問修飾符在大多數資料中的定義:訪問僅限於包含類或從包含類派生的類型。包含類指的父類 protected void Test4() { } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("初始化子類構造函數"); } public override void Test1() { Console.WriteLine("我是子類的Test1"); } public new void Test2() { Console.WriteLine("我是子類的Test2"); } public new void Test3() { Console.WriteLine("我是子類的Test3"); } } static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Console.WriteLine("-------(1)、new ParentClass()用於調用的都是基類 Begin-----------"); //=》調用的是基類。 ParentClass b1 = new ParentClass(); b1.Test1(); ParentClass b2 = new ParentClass(); b2.Test2(); ParentClass b3 = new ParentClass(); b3.Test3(); Console.WriteLine("-------END-----------"); Console.WriteLine("-------(2)、override關鍵字與父類的virtual 關鍵字 Begin-----------"); //=>override 關鍵字,重寫父類的方法。只要 new ChildClass()后,不管對象轉化誰調用的都是子類重寫方法。 ParentClass p1 = new ChildClass(); p1.Test1(); ChildClass c1 = new ChildClass(); c1.Test1(); Console.WriteLine("-------END-----------"); Console.WriteLine("-------(3)、new 關鍵字 Begin-----------"); ParentClass p2 = new ChildClass(); p2.Test2(); ChildClass c2 = new ChildClass(); c2.Test2(); Console.WriteLine("-------END-----------"); Console.WriteLine("-------(4)、new 關鍵字與父類的virtual Begin-----------"); //=>new 關鍵字,就是獨立子類與父類的相同方法,轉化為誰后調用的就是誰。 ParentClass p3 = new ChildClass(); p3.Test3(); ChildClass c3 = new ChildClass(); c3.Test3(); Console.WriteLine("-------END-----------"); Console.ReadKey(); } } }
輸出:
-------(1)、new ParentClass()用於調用的都是基類 Begin----------- 初始化父類構造函數 我是基類的Test1 初始化父類構造函數 我是基類的Test2 初始化父類構造函數 我是基類的Test3 -------END----------- -------(2)、override關鍵字與父類的virtual 關鍵字 Begin----------- 初始化父類構造函數 初始化子類構造函數 我是子類的Test1 初始化父類構造函數 初始化子類構造函數 我是子類的Test1 -------END----------- -------(3)、new 關鍵字 Begin----------- 初始化父類構造函數 初始化子類構造函數 我是基類的Test2 初始化父類構造函數 初始化子類構造函數 我是子類的Test2 -------END----------- -------(4)、new 關鍵字與父類的virtual Begin----------- 初始化父類構造函數 初始化子類構造函數 我是基類的Test3 初始化父類構造函數 初始化子類構造函數 我是子類的Test3 -------END-----------