添加Lesson16
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user