因此,假设我们有一个名为Car的类和其他三个类(Opel,Volkswagen和Peugeot)继承Car的信息,但每个类都有一个特定的变量.
所以我创建了一个新的汽车列表,我在其中添加了这三种类型的CarOpel..etc.
Opel CarOpel = new Opel(parameters);
Run Code Online (Sandbox Code Playgroud)
等等...
List<Car> Cars = new List<Car>();
Car.Add(CarOpel);
Run Code Online (Sandbox Code Playgroud)
等等...
当我使用"foreach"语句时,如何从每个类访问这些特定变量
foreach ( Car car in Cars )
{
// how to convert *car* in *Opel* , *Volkswagen* or *Peugeot* to get that specific variable?
}
Run Code Online (Sandbox Code Playgroud)
?