添加Lesson7

This commit is contained in:
2025-10-13 19:38:59 +08:00
parent ecf5241ed6
commit 3c4123d514
5 changed files with 176 additions and 0 deletions
+12
View File
@@ -29,6 +29,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束", "Les
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束练习", "Lesson6_泛型约束练习\Lesson6_泛型约束练习.csproj", "{3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束练习", "Lesson6_泛型约束练习\Lesson6_泛型约束练习.csproj", "{3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List", "Lesson7_List\Lesson7_List.csproj", "{D63FDE7B-2A03-4A86-9E4F-223AF31F099B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List练习", "Lesson7_List练习\Lesson7_List练习.csproj", "{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -87,6 +91,14 @@ Global
{3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
{3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Release|Any CPU.Build.0 = Release|Any CPU {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Release|Any CPU.Build.0 = Release|Any CPU
{D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Release|Any CPU.Build.0 = Release|Any CPU
{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
+10
View File
@@ -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>
+82
View File
@@ -0,0 +1,82 @@
namespace Lesson7_List
{
internal class Program
{
static void Main(string[] args)
{
#region List的本质
//List是C#为我们封装好的一个类
//它的本质是一个可变类型的泛型数组
//List类帮助我们实现了很多方法
//比如泛型数组的增删查改
#endregion
#region
//需要引用命名空间System.Collections.Generic
List<int> list = new List<int>();
List<string> list2 = new List<string>();
#endregion
#region
//增
list.Add(1);
list.Add(2);
list.Add(3);
list2.Add("123");
//范围增
List<string> list3 = new List<string>();
list3.Add("456");
list2.AddRange(list3);
//插入
list.Insert(0,9999);
//删
//1.移除指定元素
list.Remove(1);
//2.移除指定位置的元素
list.RemoveAt(0);
//3.清空
list.Clear();
//查
//1.得到指定位置的元素
Console.WriteLine(list[0]);
//2.查看元素是否存在
if (list.Contains(1))
{
Console.WriteLine("存在为1的元素");
}
//3.正向查找元素位置
//找到返回位置,找不到返回-1
int index = list.IndexOf(1);
Console.WriteLine(index);
//4.反向查找元素位置
//找到返回位置,找不到返回-1
index = list.LastIndexOf(1);
Console.WriteLine(index);
//改
Console.WriteLine(list[0]);
list[0] = 99;
Console.WriteLine(list[0]);
#endregion
#region
//长度
Console.WriteLine(list.Count);
//容量
//避免产生垃圾
Console.WriteLine(list.Capacity);
for (int i = 0; i < list.Count; i++)
{
Console.Write(list[i]);
}
foreach (Object item in list)
{
Console.WriteLine(item);
}
#endregion
}
}
}
@@ -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>
+62
View File
@@ -0,0 +1,62 @@
namespace Lesson7_List练习
{
#region
abstract class Monster
{
public static List<Monster> monsters = new List<Monster>();
public Monster()
{
monsters.Add(this);
}
public abstract void Atk();
}
class Boss: Monster
{
public override void Atk()
{
Console.WriteLine("Boss攻击");
}
}
class Goblin : Monster
{
public override void Atk()
{
Console.WriteLine("哥布林攻击");
}
}
#endregion
class Program
{
static void Main(string[] args)
{
#region
List<int> l1 = new List<int>();
for (int i = 10; i > 0; i--)
{
l1.Add(i);
}
l1.RemoveAt(4);
for (int i = 0; i < l1.Count; i++)
{
Console.WriteLine(l1[i]);
}
Console.WriteLine("_______________________________");
#endregion
#region
Monster m1 = new Boss();
Monster m2 = new Goblin();
Boss b = new Boss();
Goblin g = new Goblin();
for (int i = 0;i < Monster.monsters.Count;i++)
{
Monster.monsters[i].Atk();
}
#endregion
}
}
}