简体   繁体   English

Java中的多个继承中的接口,建模问题

[英]interfaces in multiple inheritance in java, modelling issue

I have the following scenario: 我有以下情况:

在此处输入图片说明

As you can see, I have two classes that are Lecturers and Students. 如您所见,我有两个班级,分别是讲师和学生。 The class Teacher Assistants are a mix between Lectures and Students, that is because they can enrolled into courses, but they can also lecture some basic topics (without being considered Lecturers). 班级助教是讲课和学生之间的混合体,这是因为他们可以报名参加课程,但他们也可以讲一些基本主题(不被视为讲师)。 I came with the idea to model this situation using Interfaces because I will program in in Java. 我想到了使用接口对这种情况建模的想法,因为我将使用Java进行编程。 Is this modelling correct? 这个模型正确吗?

在此处输入图片说明

So that the TA class will implement the Interface Teaches, which contains an array of the courses assigned to this student to teach. 这样TA班将实现接口教学,其中包含分配给该学生教书的一系列课程。

But if I model in that way I realize that I am loosing the class Lecturers at all. 但是,如果我以这种方式建模,我就会意识到自己完全失去了全班讲师。 How I can model this situation of multiple inheritance, but not loosing the class Lecturers? 我如何才能为这种多重继承的情况建模,但又不失去全班讲师的职责? I mean if I program Lecturers as an interface it would not have any methods that I would need further, for example, calculus of its wage benefits and so on. 我的意思是,如果我将讲师编程为接口,它将没有任何我需要进一步使用的方法,例如,计算其工资福利等。 Any recommendation? 有什么建议吗?

You can make Lecturer into an interface, then have both TeachingAssistant and Professor implement it. 您可以将Lecturer为一个界面,然后由TeachingAssistantProfessor实施。 TeachingAssistant can extend Student , because from a logical point of view, teaching assistants are students. TeachingAssistant可以扩展Student ,因为从逻辑的角度来看,助教是学生。

One option is to have Teacher and Teachable interfaces and then have Lecturer implement Teacher (for want of a word like Teacherable!), Student implement Teachable and TA implement both Teacher and Teachable . 一种选择是拥有TeacherTeachable接口,然后让Lecturer实现Teacher (需要像Teacherable这样的单词), Student实现TeachableTA实现TeacherTeachable

Lecturer , Student and TA could all extend Faculty_Staff . LecturerStudentTA均可扩展Faculty_Staff

tbodt's approach , in my opinion, is generally sound. 我认为, tbodt的做法总体上是合理的。 You just have to ask: if some client code needs a Lecturer , would an instance of a TA suffice? 您只需要问:如果某些客户端代码需要Lecturer ,TA实例就足够了吗? If there's an issue with that, you can choose to design it with composition, abstracting the ability to teach into a separate class of its own, something like TeachingJob (I can't think of a better name). 如果存在问题,您可以选择按构图设计,将其教学能力抽象为一个单独的类,例如“ TeachingJob (我想不出更好的名字)。 This way the only thing that actual Professor s and TeachingAssistant s share is that they have similar teaching jobs; 这样,实际的ProfessorTeachingAssistant共享的唯一东西就是他们具有相似的教学工作。 they do not belong to the same "class" (interface actually). 它们不属于同一“类”(实际上是接口)。

Hybrid approach would be to involve TeachingJob with the Lecturer interface, eg Lecturer would have a assign(TeachingJob assignment) method. 混合的方法是涉及TeachingJobLecturer接口,例如Lecturer将有一个assign(TeachingJob assignment)方法。

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

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