开发者

Can anyone help me with a question about Interfaces in C# [closed]

Closed. This question does not meet Stack Overflow guidelin开发者_JS百科es. It is not currently accepting answers.

Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist

Closed 9 years ago.

Improve this question

I've been given a question that says this.

Create the IPeople Interface: Teachers and Students have much in common but some minor differences. Create an interface to expose all commanlity between them. You will need to modify the Teacher and Student partial classes in order for them to implement this interface.

Now I'm not entirely sure what this is telling me to do. Teachers are made up of : TeacherId, FirstName, LasteName, DateOfBirth, AnnualSalary, DepartmentId and Students are made up of : StudentId, FirstName, LastName, DateOfBirth, AverageScore, DepartmentId.

I would guess then that the commanality is FirstName, LastName, DateOfBirth and DepartmentId. Also a Teacher teaches a Course and Student takes a Course.

Now I believe I would need to modify the Partial Class of Teacher and Partial Class of Student to add in the CourseID to the class so that I can also create a link between the two as I need to in a further question show all IPeople on a course. I take it an interface can display common values from two classes to create one list which can then be filtered.

So my question is. Am I getting this right so far? I've got no experience of this kind of work. If anyone could point me in the right direction for creating an interface that that would be great. Any example coding or websites.


A full answer here would take a lot of space. Entire chapters of many books have been devoted to topics like this. In general though, you are on the right track.

The problem you are likely having is that you are thinking about the design at too deep a level. That isn't your fault really. This question itself is troubling, and indicates that the person asking it is probably presenting the material at the wrong level of abstraction too.

The question's choice of IPeople isn't a good start (it should be IPerson), then it goes on to ask you to do OO design at a detailed level where you should be thinking about it from a much higher level. You should be thinking things like "student and teacher are both a kind of person", not thinking things like "student and teacher both have a field named LasteName".

Let me give you an example of how this question's too-deep level of abstraction is problematic. Thinking about it the way the question is asked means you will create an IPerson interface to represent the abstract concept of "a person". Student and Teacher both inherit this IPerson. So you design an interface that includes "all commonalities" between student and teacher (like the question says to do) and you come up with StudentId, FirstName, LastName, DateOfBirth, AverageScore, DepartmentId. (as you indicated).

At that level of detail that is as good as it gets. But consider this: later you might need to add a class for the student's parents, thus giving you a third 'kind' of person. Would it make sense for Parent to have a DepartmentId field?

By thinking about the design at too deep a level of detail like this question encourages, you can (and will) end up with horribly brittle designs that don't make sense when viewed from a higher level of abstraction.


To satisfy this, all you have to do is create the interface w/ the common properties you've identified, then in the partial classes have the two classes implement the interface, e.g.:

public partial class Student : IUniversityPerson{ ... }

where IUniversityPerson is the interface you've created. Linking course and so on isn't in the scope of the question as I read it.


You've identified the common properties / members of the Teacher and Student objects. You dont need to add more unless the problem specifies to do so - simply make the interface for the intersecting members. You're about 50% there, so now you just need to make the interface and mark each class to implement it.


Yes, you are on the right track... The commonality here is that Teacher and Student are both People. So, you could create an IPerson interface, and a Person base class. Then, both student and teacher would inherit from Person base class and both implement IPerson interface.

This kind of division allows you to separate the code needed for handling the requirements for a general "person" into separate modules. It also allows for easier maintenance, as anytime a change is needed to the common "person" attributes, it's only needed in one place. These are some of the basic principles of Object-Oriented programming.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜