我有一节课:
public class Person {
public string FirstName = "";
public string LastName = "";
}
Run Code Online (Sandbox Code Playgroud)
和派生类:
public class HRPerson : Person {
public string GetSomething() {
//calculate something
}
}
Run Code Online (Sandbox Code Playgroud)
本质上,我想扩展基类的功能.使用看起来像这样,GetAllPerson返回List<Person>.
class Program
{
static List<HRPerson> GetAllHRPerson()
{
List<HRPerson> HRPersonList = new List<HRPerson>();
foreach (Person person in GetAllPerson)
{
HRPersonList.Add(person);
}
return HRPersonList;
}
}
Run Code Online (Sandbox Code Playgroud)
它没有编译,说参数没有重载,当我尝试将人员转换为时HRPerson,我得到运行时错误"无法将类型的对象转换Person为类型HRPerson"错误.
如何添加这样的附加功能?