C#列表< T>添加继承项 [英] C# List<T> adding inherited items

查看:62
本文介绍了C#列表< T>添加继承项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有2个班级.第一个是人

Let's say that we have 2 classes. The first one is Person

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace People
{
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

第二个是老师

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace People
{
    class Teacher:Person
    {
        public string Position { get; set; }
    }
}

我想在列表中添加老师

Teacher teacher = new Teacher() {FirstName="Peter", LastName="Janson",Position="boss" };
            List <Person> people= new List<Person> { };
            people.Add(teacher);

当此列表是类型且人员仅具有FirstName和LastName属性而"teacher"也具有Position属性时,如何在列表中添加教师?

How is it possible that I add the teacher in the list when this list is of type and person only has FirstName and LastName properties and the "teacher" has also a Position property?

推荐答案

根据您之前的问题,我认为您在理解多态性方面遇到了一些问题.

Based on your previous question, I think you're having some issues understanding polymorphism.

尝试将继承视为汽车与汽车之间的关系.车辆是基本类型,汽车,摩托车,飞机,卡车等是派生类型.

Try to think of inheritance as the relationship between cars and vehicles. A vehicle is a base type, and a car, a motorbike, a plane, a truck, etc. is a derived type.

public class Vehicle
{
    public string Model {get;set;}
}

想象一下你有一个飞机机库:

Imagine you have an aeroplane hangar:

List<Vehicle> hangar = new List<Vehicle>();

您可以在飞机机库中停放多辆不同的车辆,因为它很大:

You can park multiple different vehicles in an aeroplane hangar because it's very big:

hangar.Add(new Plane());
hangar.Add(new Car());

尽管它们是不同的类型,但它们仍然是从车辆继承的,因此可以将它们存储为车辆.

Although they're different types, they still inherit from vehicle, so they can be stored as vehicles.

问题是,飞机有机翼而汽车没有.如果您只是乘坐第一辆车hangar[0],您就知道这是一辆车,并且可以获得有关它的基本信息(所有车辆共有的信息):hangar[0].Model,但是您必须更具体地了解车的类型如果您需要更多详细信息,请访问.

The thing is, planes have wings and cars don't. If you just take the first vehicle hangar[0], you know it's a vehicle and you can get the basic information about it (information that's common to all vehicles): hangar[0].Model, but you have to be more specific about the type of vehicle you're accessing if you want more detailed information.

现在,由于您可能不知道它是什么类型的车辆,因此需要检查:

Now, since you might not know what type of vehicle it is, you need to check:

if (hangar[0] is Car)
{
    string registrationNumber = ((Car)hangar[0]).RegistrationNumber;
}
else if (hangar[0] is Plane)
{
    int numberOfWings = ((Plane)hangar[0]).NumberOfWings;
}

使用C#7语法,您还可以使用以下简化形式:

Using the C#7 syntax you can also use this simplified form:

if (hangar[0] is Car car)
{
    string registrationNumber = car.RegistrationNumber;
}
else if (hangar[0] is Plane plane)
{
    int numberOfWings = plane.NumberOfWings;
}

与现实生活的类比是,如果您进入机库,则必须看一下汽车在哪里,飞机在哪里.这里是一样的.

The analogy with real life is that if you enter the hangar, you have to look to see where the car is, and where the plane is. It's the same here.

多态性使您可以将许多派生类视为它们的共同基础.在您的人员示例中,如果您希望能够按名称搜索某人,这将很有用.老师是一个人,医生是一个人,他们都是人,而且都有名字,因此在这种情况下,您可以像对待他们一样对待.

Polymorphism lets you treat many derived classes as their common base. In your person example, this would be useful if you wanted to be able to search for someone by name. A teacher is a person, a doctor is a person, they are all people and they all have names, so in that situation you can treat them the same.

将其视为快速约会-您去找人.您首先要做什么?您问对方的名字.然后,您问:您以谋生为生吗?"他们说我是老师"或我是医生".现在,您知道它们是什么类型,可以问他们您教什么?"或您专门研究医学的哪个分支?"

Think of it like speed dating - you go and you meet people. What do you do first? You ask the other person's name. Then you ask "What do you do for a living?" and they say "I'm a teacher" or "I'm a doctor". Now you know what type they are, and you can ask them "What do you teach?" or "What branch of medicine do you specialise in?"

我希望这可以使您更清楚:-)

I hope this makes it clearer for you :-)

这篇关于C#列表&lt; T&gt;添加继承项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆