diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln index 45feda1..9d748da 100644 --- a/C#进阶/C#进阶.sln +++ b/C#进阶/C#进阶.sln @@ -25,6 +25,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson5_泛型", "Lesson5_ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson5_泛型练习", "Lesson5_泛型练习\Lesson5_泛型练习.csproj", "{524E1BC4-FE9A-4186-B7EE-A04323E98696}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束", "Lesson6_泛型约束\Lesson6_泛型约束.csproj", "{79ACEF40-536F-48BC-B53F-92FEBD479704}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束练习", "Lesson6_泛型约束练习\Lesson6_泛型约束练习.csproj", "{3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -75,6 +79,14 @@ Global {524E1BC4-FE9A-4186-B7EE-A04323E98696}.Debug|Any CPU.Build.0 = Debug|Any CPU {524E1BC4-FE9A-4186-B7EE-A04323E98696}.Release|Any CPU.ActiveCfg = Release|Any CPU {524E1BC4-FE9A-4186-B7EE-A04323E98696}.Release|Any CPU.Build.0 = Release|Any CPU + {79ACEF40-536F-48BC-B53F-92FEBD479704}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79ACEF40-536F-48BC-B53F-92FEBD479704}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79ACEF40-536F-48BC-B53F-92FEBD479704}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79ACEF40-536F-48BC-B53F-92FEBD479704}.Release|Any CPU.Build.0 = Release|Any CPU + {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#进阶/Lesson6_泛型约束/Lesson6_泛型约束.csproj b/C#进阶/Lesson6_泛型约束/Lesson6_泛型约束.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson6_泛型约束/Lesson6_泛型约束.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson6_泛型约束/Program.cs b/C#进阶/Lesson6_泛型约束/Program.cs new file mode 100644 index 0000000..e487c19 --- /dev/null +++ b/C#进阶/Lesson6_泛型约束/Program.cs @@ -0,0 +1,185 @@ +namespace Lesson6_泛型约束 +{ + #region 知识回顾 + //泛型类 + class TestClass + { + public T t; + public U u; + + public U TestFun(T t) + { + return default(U); + } + + //泛型函数 + public V TestFun(K k) + { + return default(V); + } + } + #endregion + + #region 知识点一 什么是泛型约束 + //让泛型的类型有一定限制 + //关键字:where + //泛型约束一共有6种 + //1.值类型 where 泛型字母:struct + //2.引用类型 where 泛型字母:class + //3.存在无参公共构造函数 where 泛型字母:new() + //4.某个类本身或其派生类 where 泛型字母:类名 + //5.某个接口的派生类 where 泛型字母:接口名 + //6.另一个泛型类本身或派生类 where 泛型字母:另一个泛型字母 + #endregion + + #region 知识点二 各泛型约束讲解 + class Test1 where T : struct + { + public T v; + + public void TestFun(K v) where K : struct + { + + } + } + + class Test2 where T : class + { + public T value; + public void TestFun(K v) where K : class + { + + } + } + + + class Test3 where T: new() + { + public T value; + public void TestFun(K v) where K : new() + { + + } + } + class Test1 + { + + } + class Test2 + { + public Test2(int i) + { + + } + } + + + class Test4 where T : Test1 + { + public T value; + public void TestFun(K v) where K : Test1 + { + + } + } + class Test3 : Test1 + { + + } + + + interface IFly + { + + } + + class Test4 : IFly + { + + } + class Test5 where T : IFly + { + public T value; + public void TestFun(K v) where K : IFly + { + + } + } + + + class Test6 where T : U + { + public T value; + public void TestFun(K k) where K : V + { + + } + } + #endregion + + #region 知识点三 约束的组合使用 + class Test7 where T : class, new() + { + + } + #endregion + + #region 知识点四 多个泛型有约束 + class Test8 where T:class,new() where K : struct + { + + } + #endregion + + #region 总结 + //泛型约束: 让类型有一定限制 + //class + //struct + //new() + //类名 + //接口名 + //另一个泛型字母 + + //注意: + //1.可以组合使用 + //2.多个泛型约束 用where连接即可 + #endregion + internal class Program + { + static void Main(string[] args) + { + TestClass t = new TestClass(); + t.t = "123fgd"; + t.u = 10; + + t.TestFun("123"); + t.TestFun(123); + + + //Test1 t1 = new Test1;//Object类型不能为空,所以报错 + Test1 t1 = new Test1();//int是值类型,所以可以放 + //t1.TestFun();//不可为空同样报错 + t1.TestFun(1.3f); + + + //Test2 t2 = new Test2;//因为是引用类型所以值类型int会报错 + Test2 t2 = new Test2(); + t2.TestFun("123"); + t2.TestFun(new object()); + t2.value = new Random(); + + + Test3 t3 = new Test3();//如果Test1的构造函数是私有也会报错 + //Test3 t4 = new Test3();//test2有参所以报错,如果test2是无参但抽象类也会报错,必须是有公共无参构造函数的非抽象类 + //Test3 t4 = new Test3();//没问题,struct值类型是默认有无参构造且不会被顶替的 + + Test4 t4 = new Test4();//Test3是继承约束的Test1的类,所以也不会报错 + + Test5 t5 = new Test5(); + t5.value = new Test4();//继承IFly的Test4 + + Test6 t6 = new Test6 (); + Test6 tx = new Test6(); + } + } +} diff --git a/C#进阶/Lesson6_泛型约束练习/Lesson6_泛型约束练习.csproj b/C#进阶/Lesson6_泛型约束练习/Lesson6_泛型约束练习.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson6_泛型约束练习/Lesson6_泛型约束练习.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson6_泛型约束练习/Program.cs b/C#进阶/Lesson6_泛型约束练习/Program.cs new file mode 100644 index 0000000..79747f1 --- /dev/null +++ b/C#进阶/Lesson6_泛型约束练习/Program.cs @@ -0,0 +1,182 @@ +using System; + +namespace Lesson6_泛型约束练习 +{ + #region 单例模式基类 + class Test + { + private static Test instance = new Test(); + public int value = 10; + public static Test Instance + { + get + { + return instance; + } + } + private Test() { } + } + + class SingleBase where T : new() + { + private static T instance = new T(); + public static T Instance + { + get + { + return instance; + } + } + } + + class GameManager : SingleBase + { + public GameManager() + { + + } + } + #endregion + + #region 利用泛型知识点,仿造ArrayList实现一个不确定数组类型的类 并 实现增删查改的方法 + class ArrayList + { + private T[] array; + + private int count; + public ArrayList() + { + count = 0; + array = new T[16]; + } + public void Add(T value) + { + if (count >= array.Length) + { + T[] temp = new T[Capacity * 2]; + for (int i = 0; i < Capacity; i++) + { + temp[i] = array[i]; + } + array = temp; + } + array[count] = value; + count++; + } + + public void Remove(T value) + { + int index = -1; + for (int i = 0; i < count; i++)//小于实际数量不小于容量 + { + if (array[i].Equals(value))//不能写成array[i] == value因为不是所有类型都重载 + { + index = i; + break; + } + } + if (index != -1) + { + for (;index < Count - 1;index++) + { + array[index] = array[index + 1]; + } + array[Count - 1] = default(T); + count--; + } + } + + public void RemoveAt(int index) + { + if (index < 0 || index >= count) + { + Console.WriteLine("索引不合法"); + return; + } + for (; index < Count - 1; index++) + { + array[index] = array[index + 1]; + } + array[Count - 1] = default(T); + count--; + } + + public T this[int index] + { + get + { + if (index < 0 || index >= count) + { + Console.WriteLine("索引不合法"); + return default(T); + } + return array[index]; + } + set + { + if (index < 0 || index >= count) + { + Console.WriteLine("索引不合法"); + return; + } + array[index] = value; + } + } + /// + /// 得容量和实际数量 + /// + public int Capacity + { + get + { + return array.Length; + } + } + public int Count + { + get + { + return count; + } + } + } + #endregion + class Program + { + static void Main(string[] args) + { + ArrayList array = new ArrayList(); + Console.WriteLine(array.Count); + Console.WriteLine(array.Capacity); + array.Add(1); + array.Add(2); + array.Add(3); + Console.WriteLine(array.Count); + Console.WriteLine(array.Capacity); + Console.WriteLine(array[1]); + Console.WriteLine(array[-1]); + Console.WriteLine(array[5]); + array.Remove(2); + Console.WriteLine(array.Count); + Console.WriteLine("/////////////////"); + for (int i = 0; i < array.Count; i++) + { + Console.Write(array[i]); + } + Console.WriteLine(); + Console.WriteLine("/////////////////"); + array.RemoveAt(0); + for (int i = 0; i < array.Count; i++) + { + Console.Write(array[i]); + } + Console.WriteLine(); + Console.WriteLine("/////////////////"); + array[1] = 555; + for (int i = 0; i < array.Count; i++) + { + Console.Write(array[i]); + } + } + } +}