问题描述
我应该在我的 java 项目中有一个动物的层次结构,但我对应该扩展什么感到困惑.以下是说明:
I'm supposed to have a hierarchy of animals in my java project, but I'm confused on what should extend what.Here are the instructions:
编写类或接口来表示以下内容:
- 可采用
- 动物
- 蝙蝠
- 鸟
- 蓝鲸
- 狗
- 鸸鹋
- 鱼
- 金鱼
- 哺乳动物
- 水獭
- 鹦鹉
- 爬行动物
- 乌龟
- 水上乐园
- 鲸鱼
- 有翼
您需要决定类/接口的结构.考虑:
You need to decide on the structure of the classes/interfaces. Consider:
哪个应该是抽象类?一个具体的类?哪个应该是一个接口?类应该如何通过继承关联?方法应该放在哪些类中?应该用什么方法被覆盖?哪些信息应该作为参数和哪些信息可以硬编码到一个类中?一些额外的详细信息/要求:
Which should be an abstract class? a concrete class? Which should be an interface? How should the classes be related through inheritance? In what classes should methods be placed? What methods should be overridden? What information should be taken in as a parameter and what information can be hard-coded into a class? Some additional details/requirements:
所有动物都有一个返回布尔值的方法isWarmBlooded".这方法可以直接在类中,也可以继承.所有的动物都有一个名称.所有类都有一个 toString 方法,该方法返回动物的名称,动物是否是温血动物,以及所有动物的清单适用于动物的名称.toString 方法可以在类直接或继承.可以作为宠物收养的动物有一个方法getHomeCareInstructions",它返回如何照顾动物.生活在水中的动物(是水居民)有一个方法livesOnLand",它返回一个布尔值动物是否也能在陆地上生活.有翅膀的动物有一个方法飞行",返回一个布尔值动物是否可以飞行.这部分作业不一定难编程视角.你应该花时间做的是仔细考虑你的类的设计以及它们应该如何相关通过继承或接口
All animals have a method "isWarmBlooded" that returns a boolean. The method can be in the class directly or inherited. All animals have a name. All classes have a toString method that returns the animal's name, whether the animal is warm blooded, and a list of all animal names that apply to the animal. The toString method can be in the class directly or inherited. Animals that can be adopted as pets have a method "getHomeCareInstructions" that returns a description of how to care for the animal. Animals that live in water (are water dwellers) have a method "livesOnLand" that returns a boolean of whether the animal also can live on land. Animals that have wings have a method "flies" that returns a boolean of whether the animal can fly. This part of the assignment isn't necessarily difficult from a programming perspective. What you should spend time on is carefully considering the design of you classes and how they should be related through inheritance or interfaces
我不知道如何设计它,因为我知道鸟有翅膀,但蝙蝠也是.因此蝙蝠会伸出翅膀,但蝙蝠也是哺乳动物.我不能有翅膀的哺乳动物,因为鸟类不是哺乳动物.此外,鲸鱼和水獭是水栖动物,但也是水栖动物.我不能让水生动物扩展哺乳动物(因为鲸鱼/水獭是哺乳动物),但鱼不是哺乳动物,而是水生动物.我将如何使蝙蝠既是有翼又是哺乳动物?编程是简单的部分,只是在项目结构上挣扎.我该如何构建它才能使其正常工作?
I'm not sure how to design this because I know a bird is winged, but so is a bat. Therefor bat would extend winged, but bats are also mammals. I can't have winged extend mammal because birds are not mammals. Also, whales and otters are watter dwellers, but are also waterdwellers. I can't have waterdwellers extend mammals (because whales/otters are mammals), but fish are not mammals and are waterdwellers. How would I make it so a bat is both winged and a mammal? The programming is the easy part, just struggling with the structure of the project. How can I structure this so it could work?
推荐答案
所以在你的建模中你必须考虑:
So in your modelling you have to think:
哪些是真正的动物?
例如鲸鱼、水獭 -> 课程
e.g. whales, otters -> classes
哪些是动物?
例如一只鸟.-> 抽象类
e.g. a bird. -> abstract class
因为每只鸸鹋都是一只鸟.
这称为 Liskov 替换原则 https://en.wikipedia.org/wiki/Liskov_substitution_principle
Since every emu is a bird.
This is called the Liskov Substitution Principle https://en.wikipedia.org/wiki/Liskov_substitution_principle
动物的特征是什么?
例如WaterDweller、Winged -> 那些是接口.
e.g. WaterDweller, Winged -> those are interfaces.
每个班级都可以有一个超班级,它可以有许多特征,比如有翅膀,或者是水居者,或者是可收养的
Every class can have one superclass, it can have many characteristics, like being winged, or being a waterdweller, or adoptable
蝙蝠示例的一个补充:
它是一种哺乳动物 -> 因此它的超类是哺乳动物.它是有翼的——这是一个特性——所以它实现了有翼的接口.
It is a mammal -> therefore it's superclass is mammal. It is winged - which is a characteristic - so it implements the winged interface.
class Bat extends Mammal implements Winged{
}
这篇关于Java继承层次动物类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!