添加Lesson16

This commit is contained in:
2025-09-11 13:53:18 +08:00
parent 8dcaa99ff4
commit aca44bc248
5 changed files with 172 additions and 0 deletions
+12
View File
@@ -27,6 +27,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson15_继承-万物之
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson15_继承-万物之父和装箱拆箱练习", "Lesson15_继承-万物之父和装箱拆箱练习\Lesson15_继承-万物之父和装箱拆箱练习.csproj", "{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_继承-密封类", "Lesson16_继承-密封类\Lesson16_继承-密封类.csproj", "{C68D86B1-87E3-47FC-BF5B-B9B39E1B0BD1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_继承-密封类练习", "Lesson16_继承-密封类练习\Lesson16_继承-密封类练习.csproj", "{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -81,6 +85,14 @@ Global
{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}.Release|Any CPU.Build.0 = Release|Any CPU
{C68D86B1-87E3-47FC-BF5B-B9B39E1B0BD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C68D86B1-87E3-47FC-BF5B-B9B39E1B0BD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C68D86B1-87E3-47FC-BF5B-B9B39E1B0BD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C68D86B1-87E3-47FC-BF5B-B9B39E1B0BD1}.Release|Any CPU.Build.0 = Release|Any CPU
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Release|Any CPU.ActiveCfg = Release|Any CPU
{673FABF2-99A7-4E63-B1F6-37AB53B6EC85}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson16_继承_密封类</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,34 @@
namespace Lesson16_继承_密封类
{
#region
//密封类 是使用 sealed密封关键字修饰的类
//作用:让类无法再被继承
#endregion
#region
class Father
{
}
sealed class Son : Father//之后无法再被继承
{
}
#endregion
#region
//在目前的程序设计中,密封类的主要作用就是不允许最底层子类被继承
//可以保证程序的规范性
//目前对于我们来说 可能用处不大
#endregion
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson16_继承_密封类练习</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,104 @@
namespace Lesson16_继承_密封类练习
{
class Vehicle
{
public int speed;
public int maxSpeed;
public int maxCapacity;
public int nowMemberindex;
public Human[] passenger;
public Vehicle(int speed, int maxSpeed, int maxCapacity)
{
this.speed = speed;
this.maxSpeed = maxSpeed;
this.nowMemberindex = 0;
this.maxCapacity = maxCapacity;
passenger = new Human[maxCapacity];
}
public void GetOn(Human p)
{
if (nowMemberindex >= maxCapacity)
{
Console.WriteLine("乘客已满");
return;
}
else
{
Console.WriteLine("已上车");
passenger[nowMemberindex] = p;
nowMemberindex++;
}
}
public void GetOff(Human p)
{
for (int i = 0; i < nowMemberindex; i++)
{
if (passenger[i] == p)
{
for (int j = i; j < nowMemberindex - 1; j++)
{
passenger[j] = passenger[j + 1];
}
passenger[nowMemberindex - 1] = null;
nowMemberindex--;
Console.WriteLine("已下车一位成员");
break;
}
}
}
public void Moving()
{
}
public void Accident()
{
}
}
class Human
{
}
class Driver : Human
{
}
class Passenger : Human
{
public int age;
public string name;
public Passenger()
{
}
public Passenger(int age,string name)
{
this.age = age;
this.name = name;
}
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Human h1 = new Passenger(1,"h1");
Human h2 = new Passenger(2,"h2");
Human h3 = new Passenger(3,"h3");
Vehicle Car = new Vehicle(20,200,2);
Car.GetOn(h1);
Car.GetOn(h2);
Car.GetOn(h3);
Car.GetOff(h1);
Car.GetOn(h3);
Car.GetOff(h3);
}
}
}