105 lines
2.4 KiB
C#
105 lines
2.4 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|