Java 继承等级动物 类
Java inheritance hierarchy animal classes
我的 java 项目中应该有一个动物层次结构,但我对什么应该扩展什么感到困惑。
以下是说明:
Write classes or interfaces to represent the following:
- Adoptable
- Animal
- Bat
- Bird
- BlueWhale
- Dog
- Emu
- Fish
- Goldfish
- Mammal
- Otter
- Parakeet
- Reptile
- Turtle
- WaterDweller
- Whale
- Winged
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:
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
我不确定如何设计这个,因为我知道鸟是有翅膀的,但蝙蝠也是。因此蝙蝠会长出翅膀,但蝙蝠也是哺乳动物。我不能有翅膀的哺乳动物,因为鸟类不是哺乳动物。此外,鲸鱼和水獭是水栖动物,但也是水栖动物。我不能让水栖动物扩展哺乳动物(因为 whales/otters 是哺乳动物),但鱼不是哺乳动物,而是水栖动物。我怎样才能让蝙蝠既有翅膀又是哺乳动物?编程是简单的部分,只是在项目结构上挣扎。我该如何构造它才能正常工作?
所以在你的建模中你必须考虑:
哪些是真实的动物?
例如鲸鱼、水獭 -> classes
哪些是一种动物?
例如一只鸟。 -> 摘要 class
因为每只鸸鹋都是鸟。
这就是所谓的 Liskov 替换原则 https://en.wikipedia.org/wiki/Liskov_substitution_principle
动物有哪些特征?
例如WaterDweller、Winged -> 这些是接口。
每个class都可以有一个超级class,它可以有很多特征,比如有翅膀,或者是水栖动物,或者可以收养
为您的蝙蝠示例添加一个:
它是哺乳动物 -> 因此它超级class是哺乳动物。它是有翼的——这是一个特征——所以它实现了有翼的界面。
class Bat extends Mammal implements Winged{
}
我的 java 项目中应该有一个动物层次结构,但我对什么应该扩展什么感到困惑。 以下是说明:
Write classes or interfaces to represent the following:
- Adoptable
- Animal
- Bat
- Bird
- BlueWhale
- Dog
- Emu
- Fish
- Goldfish
- Mammal
- Otter
- Parakeet
- Reptile
- Turtle
- WaterDweller
- Whale
- Winged
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:
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
我不确定如何设计这个,因为我知道鸟是有翅膀的,但蝙蝠也是。因此蝙蝠会长出翅膀,但蝙蝠也是哺乳动物。我不能有翅膀的哺乳动物,因为鸟类不是哺乳动物。此外,鲸鱼和水獭是水栖动物,但也是水栖动物。我不能让水栖动物扩展哺乳动物(因为 whales/otters 是哺乳动物),但鱼不是哺乳动物,而是水栖动物。我怎样才能让蝙蝠既有翅膀又是哺乳动物?编程是简单的部分,只是在项目结构上挣扎。我该如何构造它才能正常工作?
所以在你的建模中你必须考虑:
哪些是真实的动物?
例如鲸鱼、水獭 -> classes
哪些是一种动物?
例如一只鸟。 -> 摘要 class
因为每只鸸鹋都是鸟。
这就是所谓的 Liskov 替换原则 https://en.wikipedia.org/wiki/Liskov_substitution_principle
动物有哪些特征?
例如WaterDweller、Winged -> 这些是接口。
每个class都可以有一个超级class,它可以有很多特征,比如有翅膀,或者是水栖动物,或者可以收养
为您的蝙蝠示例添加一个:
它是哺乳动物 -> 因此它超级class是哺乳动物。它是有翼的——这是一个特征——所以它实现了有翼的界面。
class Bat extends Mammal implements Winged{
}