添加Lesson5
This commit is contained in:
@@ -21,6 +21,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson4_Hashtable", "Lesson
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson4_Hashtable练习题", "Lesson4_Hashtable练习题\Lesson4_Hashtable练习题.csproj", "{962C9536-7CD7-4F5A-9D1E-1DCF986953D4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson5_泛型", "Lesson5_泛型\Lesson5_泛型.csproj", "{8ECE6977-E08D-4089-9053-DFFD9864AB81}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson5_泛型练习", "Lesson5_泛型练习\Lesson5_泛型练习.csproj", "{524E1BC4-FE9A-4186-B7EE-A04323E98696}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -63,6 +67,14 @@ Global
|
||||
{962C9536-7CD7-4F5A-9D1E-1DCF986953D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{962C9536-7CD7-4F5A-9D1E-1DCF986953D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{962C9536-7CD7-4F5A-9D1E-1DCF986953D4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8ECE6977-E08D-4089-9053-DFFD9864AB81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8ECE6977-E08D-4089-9053-DFFD9864AB81}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8ECE6977-E08D-4089-9053-DFFD9864AB81}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8ECE6977-E08D-4089-9053-DFFD9864AB81}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{524E1BC4-FE9A-4186-B7EE-A04323E98696}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{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
|
||||
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>
|
||||
@@ -0,0 +1,136 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace Lesson5_泛型
|
||||
{
|
||||
#region 知识点一 泛型是什么
|
||||
//泛型实现了类型参数化,达到了代码重用的目的
|
||||
//通过类型参数化来实现同一份代码上操作多种类型
|
||||
|
||||
//泛型相当于类型占位符
|
||||
//定义类或方法时使用替代符代表变量类型
|
||||
//当真正使用类或方法时再具体指定类型
|
||||
#endregion
|
||||
|
||||
#region 知识点二 泛型分类
|
||||
//泛型类和泛型接口
|
||||
//基本语法:
|
||||
//class 类名<泛型占位符>
|
||||
//interface 接口名<泛型占位符>
|
||||
|
||||
//泛型占位语法
|
||||
//基本语法:函数名<泛型占位符>(参数列表)
|
||||
|
||||
//注意:泛型占位符可以有多个,用逗号分开
|
||||
#endregion
|
||||
|
||||
#region 知识点三 泛型类和接口
|
||||
class Test<T>
|
||||
{
|
||||
public T value;
|
||||
}
|
||||
|
||||
class Test2<T1, T2, K, M, L>
|
||||
{
|
||||
public T1 value1;
|
||||
public T2 value2;
|
||||
public K value3;
|
||||
public M value4;
|
||||
public L value5;
|
||||
}
|
||||
|
||||
interface TestInterface<T>
|
||||
{
|
||||
T value
|
||||
{
|
||||
get
|
||||
{
|
||||
return value;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Test4 : TestInterface<int>
|
||||
{
|
||||
//接口实现
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点四 泛型方法
|
||||
class Test5
|
||||
{
|
||||
public void TestFun<T>(T value)
|
||||
{
|
||||
Console.WriteLine(value);
|
||||
}
|
||||
public void TestFun<T>()
|
||||
{
|
||||
//用泛型类型做逻辑处理
|
||||
T t = default(T);//
|
||||
}
|
||||
|
||||
public T TestFun<T>(string v)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public void TestFun<T, K, M>(T t,K k,M m)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
class Test5<T>//泛型类和普通类即使只是多加一个泛型也是两个类
|
||||
{
|
||||
public T value;
|
||||
|
||||
//下面这个不是泛型方法,因为T是泛型类声明时指定的
|
||||
//我们就不能再手动指定类型了
|
||||
public void TestFun(T t)
|
||||
{
|
||||
|
||||
}
|
||||
public void TestFun<K>(K k)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点五 泛型的作用
|
||||
//1.不同类型的对象的相同逻辑处理可以用泛型
|
||||
//2.使用泛型可以一定程度上避免装箱拆箱
|
||||
//举例:优化ArrayList
|
||||
class ArrayList<T>
|
||||
{
|
||||
private T[] array;//实现它的增删查改
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Test<int> t1 = new Test<int>();
|
||||
t1.value = 10;
|
||||
Console.WriteLine(t1.value);
|
||||
|
||||
Test<string> t2 = new Test<string>();
|
||||
t2.value = "test";
|
||||
Console.WriteLine(t2.value);
|
||||
|
||||
Test5 t3 = new Test5();
|
||||
t3.TestFun<string>("123");
|
||||
|
||||
Test5<int> t4 = new Test5<int>();
|
||||
//t4.TestFun<("123");//不能是string了因为声明时是int
|
||||
t4.TestFun<string>("123");
|
||||
t4.TestFun<float>(1.23f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,33 @@
|
||||
namespace Lesson5_泛型练习
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
Console.WriteLine(InputType<int>());
|
||||
Console.WriteLine(InputType<float>());
|
||||
Console.WriteLine(InputType<double>());
|
||||
Console.WriteLine(InputType<string>());
|
||||
Console.WriteLine(InputType<decimal>());
|
||||
|
||||
}
|
||||
public static string InputType<T>()
|
||||
{
|
||||
if (typeof(T) == typeof(int))
|
||||
{
|
||||
return string.Format("{0},{1}字节", "整形", sizeof(int));
|
||||
}else if (typeof(T) == typeof(float))
|
||||
{
|
||||
return string.Format("{0},{1}字节", "单精度浮点数", sizeof(float));
|
||||
}else if (typeof(T) == typeof(double))
|
||||
{
|
||||
return string.Format("{0},{1}字节", "双精度浮点数", sizeof(double));
|
||||
}else if (typeof(T) == typeof(string))
|
||||
{
|
||||
return string.Format("{0},{1}字节", "字符串", "不定长");
|
||||
}
|
||||
return "其他类型";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user