Java 扩展了替代方案

发布于 2024-12-12 09:36:53 字数 573 浏览 5 评论 0原文

我正在尝试制作我们大学课程管理系统的模型(针对课程)。我偶然发现了extends的一个小限制,即某个类不可能扩展两个类。模型如下:

                     Professor
                    /                  
             Staff
          /          \                       
    Person            Tutor            
           \         /      
             Student            

因此,我得到了员工和学生扩展人员和教授扩展员工。现在我和班级导师有一个小困境,他基本上是一个学生(有学生ID),但在某种意义上是大学员工(举办小组讲座,负责某个项目等)。由于我不能使用两个扩展,那么还有什么其他替代方案,如果可能的话,有人可以用 implements 给出一个非常简单的示例吗?我已经看到了它的接口示例,但我不太确定如何在我的程序中使用它,因为 Student 不是接口?

I'm trying to make a model of our universities Course management system (for the course). I stumbled upon a small limitation of extends, that is, it's impossible for a certain class to extend two classes. Here's the model:

                     Professor
                    /                  
             Staff
          /          \                       
    Person            Tutor            
           \         /      
             Student            

So with that I get Staff and Student extending Person and Professor extending Staff. Now I have a small dilemma with class Tutor, who is basically a student (has studentID), but is in a sense University employee (holds Group lectures, responsible for a certain project, etc). Since I can't use two extends, what are other alternatives, and if possible could someone give a very simple example with implements? I've seen an example of interface of it, but I'm not exactly sure as how can I use it in my program, since Student is not an interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨小墨 2024-12-19 09:36:53

不要使用继承,使用组合:每个人都是一个人,但都有一个或多个角色:学生、导师、教授、职员。

或者可以选择在教职员工和学生之间进行划分,并允许他们担任角色。然后,角色决定他们可以做什么或不能做什么。

Don't use inheritance, use composition: everybody is a Person, but has one or more roles: Student, Tutor, Professor, Staff.

Or optionally make a division between Staff and Student and allow them to have the roles. The roles then dictate what they can or cannot do.

黯然 2024-12-19 09:36:53

你可以做这样的事情。

interface Person { ... }

interface Staff extends Person { ... }

interface Student extends Person { ... }

class Tutor implements Staff, Student { ... }

class Professor implements Staff { ... }

不幸的是,您不能将逻辑(即字段或非抽象方法)同时放入 StaffStudent 中,因为它们都需要是类,并且是 Java 类不能扩展两个类。

您可以将 Student Staff (不是两者)设置为抽象类,从而允许您将逻辑放入其中。不过,这有点不优雅,因为没有概念上的理由说明为什么一个应该是抽象类,而一个应该是接口。理想情况下,它们都应该是抽象类,但为此您需要切换到具有 MI 的语言。

interface Person { ... }

interface Staff extends Person { ... }

abstract class Student implements Person {
    long studentID;
    ...
}

class Tutor extends Student implements Staff { ... }

class Professor implements Staff { ... }

You can do something like this.

interface Person { ... }

interface Staff extends Person { ... }

interface Student extends Person { ... }

class Tutor implements Staff, Student { ... }

class Professor implements Staff { ... }

Unfortunately, you can't put logic (i.e., fields or non-abstract methods) in both Staff and Student because they would both need to be classes, and a Java class cannot extend two classes.

You could make Student or Staff (not both) an abstract class, allowing you to put logic in it. It would be kind of inelegant, though, since there's no conceptual reason why one should be an abstract class while one should be an interface. Ideally they should both be abstract classes, but for that you'd need to switch to a language with MI.

interface Person { ... }

interface Staff extends Person { ... }

abstract class Student implements Person {
    long studentID;
    ...
}

class Tutor extends Student implements Staff { ... }

class Professor implements Staff { ... }
苹果你个爱泡泡 2024-12-19 09:36:53

使 StudentStaff 接口由 Tutor 实现。

Student 将声明一个 getStudentId() 方法,Staff 将声明 getLectures()getProjects( )等等。

Make Student and Staff interfaces implemented by Tutor.

Student will declare a getStudentId() method, and Staff will declare getLectures(), getProjects() etc..

追风人 2024-12-19 09:36:53

制作人员、员工和学生界面。如果需要通用功能,请使用封装。

Make Person, Staff, and Student interfaces. If you need common functionality, use encapsulation.

堇色安年 2024-12-19 09:36:53

正如其他人所指出的,要在 Java 中执行此操作,您需要定义接口而不是超类。 Java 不允许多次扩展。

但可能有比继承更好的选择。在这种情况下,以及几乎在任何我发现自己想要让子类扩展多个超类的情况下,我都会尝试找到一种方法来通过委托而不是继承来对情况进行建模。

创建一个 Person 类,其中包含一组 Role 对象,其中 Role 是一个接口,Student>员工实施角色。那么一个人就可以拥有多种角色,而不是“成为”多种类型的人。

As others have noted, to do this in Java you need to define interfaces rather than superclasses. Java does not allow multiple extends.

But there may be a better alternative than inheritance. In this, and almost any situation where I find myself tempted to have a subclass extend multiple superclasses, I try to find a way to model the situation with delegation rather than inheritance.

Create a Person class that contains a set of Role objects where Role is an interface, and Student and Staff implement Role. Then a person can have multiple roles instead of "being" multiple types of person.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文