OOP任务(类层次结构,继承,接口等) [英] OOP Task (class hierarchy, inheritance, interface, etc.)

查看:173
本文介绍了OOP任务(类层次结构,继承,接口等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我正在努力了解有关OOP(Java)的更多信息,因此我正在研究一些我发现这个任务的文献。不幸的是,由于我对OOP很新,我没有任何样本解决方案。也许你们中的一些人可以给我一些输入,所以可以通过这个方式。

Since I am trying to learn more about OOP (Java) I'm working my way through some literature where I found this 'task'. Unfortunately I am having kind of a hard time since I am pretty new to OOP and I don't have any sample solution to this. Maybe some of you can give me some input so can work my way through this.


  1. 为这些类定义类层次结构:


    • quadrilateral

    • 凸四边形

    • trapezoid

    • parallelogram

    • 菱形

    • 矩形

    • square

  1. Define a class hierarchy for these classes:
    • quadrilateral
    • convex quadrilateral
    • trapezoid
    • parallelogram
    • rhombus
    • rectangle
    • square






01班级等级

好的,这是简单的数学和你可以在各地找到关于四边形等级的大量信息。这就是我做的:

Okay, this is simple math and you can find tons of information on the hierarchy of quadrilaterals everywhere. Here is what I did:

创建每个类的对象没什么大不了的,但是我对理解所有OOP技术仍有一些问题。有些地方我不知道最好的方法是什么...(例如,继承自两个类的广场,在java中根本不可能)。此外,公式(如计算表面积)无论如何都会被覆盖(因为它们在大多数情况下是不同的),所以为什么我还需要继承呢?难道我不能只使用一个接口,在所有这些类中使用它强迫他们实现这些公式吗?

Creating Objects of each class is no big deal, but I still have some problems with understanding all the OOP-techniques. There are some points where I don't know what would be the better way to do it... (e.g. the square which inherits from two classes, which in java is simply not possible). Also, formulas (like calculating the surface area) would be overwritten all the time anyhow (since they are different most of the time), so why would I need inheritance anyway? Couldn't I just use an interface, use it in all of those classes an force them to implement these formulas?

问候 - Vulpecula

Greetings - Vulpecula

推荐答案

在现实生活中,使用界面可能会更好。像这样的深层继承结构常常令人不悦;通常认为更喜欢构图而不是继承( http://en.wikipedia.org/wiki/Composition_over_inheritance) )。例如,您可以使用四边形界面来定义表面区域和周长,然后使其他形状满足该界面。

In real life, you probably would be better off using an interface. Deep inheritance structures like that are often frowned upon; it's generally considered good to 'prefer composition over inheritance' (http://en.wikipedia.org/wiki/Composition_over_inheritance). You might for instance have a 'quadrilateral' interface that defines 'surface area' and 'perimeter', and then have the other shapes satisfy that interface.

如果这是然而,家庭作业问题,那么你应该将课程层次结构建立在你的教科书/老师之前提供的任何例子上。这不是关于设计健壮的软件,而是要向老师证明你学会了以他们认为应该做的任何方式做事。

If this is a homework question however, then you should probably base the class hierarchy on whatever examples your textbook/teacher have provided previously. It's not about designing robust software, it's about proving to your teacher that you learned how to do things in whatever way they think you should do them.

这篇关于OOP任务(类层次结构,继承,接口等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆