简体   繁体   English

在派生类中添加属性

[英]Adding properties in derived class

So I am trying to code to an interface rather than an implementation. 因此,我尝试编写接口而不是实现的代码。 So I have a factory that returns an object that derives from Employee. 所以我有一个工厂,它返回一个从Employee派生的对象。 So these objects may be something like Developer : Employee, Secretary : Employee, etc. 因此,这些对象可能类似于“开发人员:员工”,“秘书:员工”等。

So I put all of the shared properties like FirstName, LastName, Email, etc. in the base class (Employee). 因此,我将所有共享属性(如FirstName,LastName,Email等)放在基类(Employee)中。

And I should put all of the properties specific to each type in just that type. 我应该将每种类型的所有特定属性都放在该类型中。 So Developer would have some properties in it like Skills, ProgrammingLanguages, etc. but then I will not be able to access these properties from the Employee object unless I use the concrete type of Developer. 因此,Developer中将具有一些属性,例如Skills,ProgrammingLanguages等,但是除非使用具体类型的Developer,否则我将无法从Employee对象访问这些属性。

eg 例如

Employee employee = new EmployeeFactory.CreateEmployee(EmployeeType.Developer);
employee.ProgrammingLanguages = "C#, Java, C++"; <-- compile error

What's the best way to go about this? 最好的方法是什么? Reflection...? 反射...?

Why bother with a factory if you're going to be explicitly saying what type of employee to use? 如果您要明确说出要使用哪种类型的员工,为什么还要麻烦工厂?

Instead, you can just write: 相反,您可以编写:

Developer dev = new Developer();
dev.ProgrammingLanguages = "C#, Java, C++"; // compiles fine

Later, you can still add the "developer" to a list of employees, ie: 以后,您仍然可以将“开发人员”添加到员工列表中,即:

IList<Employee> theEmployees = GetEmployees();
theEmployees.Add(dev); // This is fine, since Developer is an Employee...

Why not use generics and have something like 为什么不使用泛型并拥有类似的东西

T EmployeeFactory.CreateEmployee<T>() 
{
     return new T();
}

And call it like 并称它为

var dev = EmployeeFactory.CreateEmployee<Developer>();

That way you would end up with a typed Developer. 这样一来,您最终将得到类型化的开发人员。

In your code, clearly when you try to do employee.ProgrammingLanguages you know that employee is of type Developer . 在您的代码中,当您尝试使用employee.ProgrammingLanguages时,您会清楚地知道该employee属于Developer类型。 So you can just cast to that: 因此,您可以强制转换为:

Developer dev = new (Developer)EmployeeFactory.CreateEmployee(EmployeeType.Developer);
dev.ProgrammingLanguages = "C#, Java, C++";

Or more perhaps: 或者更多:

Developer dev = new Developer(); // If you don't really need the factory.
dev.ProgrammingLanguages = "C#, Java, C++";

In contexts where you don't know if this can be done or not, you can test with is or as . 在您不知道是否可以完成此操作的情况下,可以使用isas进行测试。

Keep things at the level they make sense to deal with them at. 将事物保持在合理的水平以应对它们。 Clearly it doesn't make sense to talk about the programming languages of someone who might not program, so its fine to work at the Developer level of the hierarchy. 显然,谈论可能不会编程的人的编程语言是没有意义的,因此可以在层次结构的Developer级别上正常工作。 Code for dealing with holidays and salary should either be the same for all employees, or at least work through a common interface with the possibility of overrides, and so it would work at the Employee level of the hierarchy. 处理假期和薪水的代码对于所有员工应该是相同的,或者至少应通过可能被覆盖的通用接口进行工作,因此它将在层次结构的Employee级别上起作用。

It seems to me like, at least in this specific case, you shouldn't be using the base type. 在我看来,至少在这种情况下,您不应该使用基本类型。

You're working with something specific to a Developer rather than a generic Employee so you lose the benefit of working with the base type. 您使用的是特定于Developer东西,而不是通用的Employee因此您失去了使用基本类型的好处。 In this case, just create a new developer: 在这种情况下,只需创建一个新的开发人员:

Developer developer = new Developer();
developer.ProgrammingLanguages = "C#, Java, C++";

That being said, you could always try a cast back to the more specific type: 话虽如此,您总是可以尝试将类型转换回更具体的类型:

Employee employee = new EmployeeFactory.CreateEmployee(EmployeeType.Developer);

Developer developer = employee as Developer;
if(developer != null) developer.ProgrammingLanguages = "C#, Java, C++";

In this case I would suggest using the simpliest solution. 在这种情况下,我建议使用最简单的解决方案。 Create one method per one employee type, like this: 为一种员工类型创建一个方法,如下所示:

public Developer CreateDeveloper();
public Secretary CreateSecretary();

Why this "ugly" solution? 为什么采用这种“丑陋”的解决方案? Because you need to know the type in the client code either way. 因为您需要以任何一种方式了解客户端代码中的类型。 So why to complicate with generics/reflection? 那么为什么要使泛型/反射复杂化呢? Simpicity is divine. 朴素是神圣的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM