使用sealed密封关键字修饰的类
作用 让类无法再被继承
sealed class Father
{
}
断子绝孙
在程序设计中 密封类的主要作用是不允许最底层子类被继承
可以保证程序的规范性,安全性。
Human数组可以存储 Driver和Passenger
遍历Human数组时只用接受一个Human类型的形参 就可以根据这个形参判断出是Driver还是Passenger
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text.Json.Serialization;
class Human
{
}
class Driver : Human
{
}
class Passenger : Human
{
}
class Bus
{
public float speed;
public float maxSpeed;
public int Available;
public int nowNumber;
Driver d;
Human[] p;
public Bus(float speed, float maxSpeed, int available)
{
this.speed = speed;
this.maxSpeed = maxSpeed;
p =new Human[available];
this.Available = available;
nowNumber = 0;
}
public void GetOn(Human someBody)
{
if(nowNumber <=Available)
{
p[nowNumber] = someBody ;
nowNumber++;
}
}
public void GetDown(Human someBody)
{
for (int i = 0; i < nowNumber; i++)
{
if (p[i]==someBody )
{
for (int j = i; j < nowNumber; j++)
{
p[i] = p[i + 1];
}
nowNumber--;
p[nowNumber] = null;
}
}
}
public void Driving()
{
}
public void Accident()
{
}
}
class Program
{
static void Main()
{
Bus bus = new Bus(10,50,10);
Human driver = new Driver();
Passenger passenger = new Passenger();
Passenger passenger1 = new Passenger();
bus.GetOn(driver);
bus .GetOn(passenger);
bus .GetOn(passenger1);
bus .GetDown(passenger);
}
}