添加Lesson6

This commit is contained in:
2025-10-13 17:44:02 +08:00
parent 24da5481a1
commit ecf5241ed6
5 changed files with 399 additions and 0 deletions
+12
View File
@@ -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
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+185
View File
@@ -0,0 +1,185 @@
namespace Lesson6_泛型约束
{
#region
//泛型类
class TestClass<T, U>
{
public T t;
public U u;
public U TestFun(T t)
{
return default(U);
}
//泛型函数
public V TestFun<K, V>(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<T> where T : struct
{
public T v;
public void TestFun<K>(K v) where K : struct
{
}
}
class Test2<T> where T : class
{
public T value;
public void TestFun<K>(K v) where K : class
{
}
}
class Test3<T> where T: new()
{
public T value;
public void TestFun<K>(K v) where K : new()
{
}
}
class Test1
{
}
class Test2
{
public Test2(int i)
{
}
}
class Test4<T> where T : Test1
{
public T value;
public void TestFun<K>(K v) where K : Test1
{
}
}
class Test3 : Test1
{
}
interface IFly
{
}
class Test4 : IFly
{
}
class Test5<T> where T : IFly
{
public T value;
public void TestFun<K>(K v) where K : IFly
{
}
}
class Test6<T,U> where T : U
{
public T value;
public void TestFun<K,V>(K k) where K : V
{
}
}
#endregion
#region 使
class Test7<T> where T : class, new()
{
}
#endregion
#region
class Test8<T,K> 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<string,int> t = new TestClass<string,int>();
t.t = "123fgd";
t.u = 10;
t.TestFun("123");
t.TestFun<float,double>(123);
//Test1<Object> t1 = new Test1<object>;//Object类型不能为空,所以报错
Test1<int> t1 = new Test1<int>();//int是值类型,所以可以放
//t1.TestFun<Random>();//不可为空同样报错
t1.TestFun<float>(1.3f);
//Test2<int> t2 = new Test2<int>;//因为是引用类型所以值类型int会报错
Test2<Random> t2 = new Test2<Random>();
t2.TestFun<string>("123");
t2.TestFun<Object>(new object());
t2.value = new Random();
Test3<Test1> t3 = new Test3<Test1>();//如果Test1的构造函数是私有也会报错
//Test3<Test2> t4 = new Test3<Test2>();//test2有参所以报错,如果test2是无参但抽象类也会报错,必须是有公共无参构造函数的非抽象类
//Test3<int> t4 = new Test3<int>();//没问题,struct值类型是默认有无参构造且不会被顶替的
Test4<Test3> t4 = new Test4<Test3>();//Test3是继承约束的Test1的类,所以也不会报错
Test5<IFly> t5 = new Test5<IFly>();
t5.value = new Test4();//继承IFly的Test4
Test6<Test4,IFly> t6 = new Test6<Test4, IFly> ();
Test6<Test4, Test4> tx = new Test6<Test4, Test4>();
}
}
}
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -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<T> where T : new()
{
private static T instance = new T();
public static T Instance
{
get
{
return instance;
}
}
}
class GameManager : SingleBase<GameManager>
{
public GameManager()
{
}
}
#endregion
#region 仿ArrayList实现一个不确定数组类型的类
class ArrayList<T>
{
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;
}
}
/// <summary>
/// 得容量和实际数量
/// </summary>
public int Capacity
{
get
{
return array.Length;
}
}
public int Count
{
get
{
return count;
}
}
}
#endregion
class Program
{
static void Main(string[] args)
{
ArrayList<int> array = new ArrayList<int>();
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]);
}
}
}
}