关于继承关系的概念问题?

发布于 2024-10-19 06:45:12 字数 590 浏览 3 评论 0原文

这是我想要解决的家庭作业问题:

R-2.9 考虑继承 练习 R-2.5 中的课程,并让 d 是 Horse 类型的对象变量。 如果 d 指的是实际对象 类型 Equestrian,可以将其投射到 赛车手级?为什么或为什么不?

这是练习 R-2.5:

R-2.5 绘制类继承图 对于以下一组类:

• 类 Goat 扩展了 Object 并添加了实例变量 tail 和 方法 milk() 和 Jump()。

• Pig 类扩展了 Object 并添加了一个实例变量 nos 和 方法 eat() 和 wallow()。

• Horse 类扩展了 Object 并添加了实例变量 height 和 颜色,以及 run() 和 Jump() 方法。

• Racer 类扩展了 Horse 并添加了 race() 方法。

• Equestrian 类扩展了 Horse 并添加了一个实例变量 重量和方法 trot() 和 isTrained()。

Here is my Homework question I want to solve:

R-2.9 Consider the inheritance of
classes from Exercise R-2.5, and let d
be an object variable of type Horse.
If d refers to an actual object of
type Equestrian, can it be cast to the
class Racer? Why or why not?

here is exercise R-2.5:

R-2.5 Draw a class inheritance diagram
for the following set of classes:

• Class Goat extends Object and adds an instance variable tail and
methods milk() and jump().

• Class Pig extends Object and adds an instance variable nose and
methods eat() and wallow().

• Class Horse extends Object and adds instance variables height and
color, and methods run() and jump().

• Class Racer extends Horse and adds a method race().

• Class Equestrian extends Horse and adds an instance variable
weight and methods trot() and
isTrained().

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

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

发布评论

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

评论(2

别忘他 2024-10-26 06:45:12

至于“马术运动员可以被选为赛车手吗?”问题的一部分:尝试实际实现所描述的类,并编写执行此类转换的代码。它能编译吗?如果是的话,运行时会抛出异常吗?

至于“为什么?”部分:如果类 B 扩展类 A,这意味着任何 B 也是 A。这是 Java 唯一知道这些类代表什么或我们打算如何使用它们的事情 - Java 只知道每个 Racer 都是一匹马,并且每个 Equestrian 也是一匹马(Java 知道这一点只是因为我们在声明类时这么说)。仅基于这些信息,Java 应该允许您选择 Equestrian(它可以做所有马都能做的事情,以及一些马术特定的事情,例如小跑)并开始将其视为赛车手,这是否合理(它也可以做所有马能做的事情,但不一定能小跑,并且可以做一些马术不能做的事情)?

另请注意,编译器允许的转换与尝试时实际执行的转换之间存在差异。一般来说,编译器将允许可能成功的强制转换。尝试每个代码段;它们中的每一个都应该给出不同的结果(一个不会编译,一个可以编译但在运行时抛出异常,一个将编译并运行而没有异常)。然后,反思一下为什么 Java 被设计成这样。

// Code segment A
Horse h = new Equestrian();
Goat g = (Goat)h;

// Code segment B
Horse h = new Equestrian();
Racer r = (Racer)h;

// Code segment C
Horse h = new Racer();
Racer r = (Racer)h;

As for the "Can an Equestrian be cast to a Racer?" part of the question: Try to actually implement the classes as described, and write code that performs such a cast. Does it compile? If so, does it throw an exception when it is run?

As for the "Why?" part: If class B extends class A, this means that any B is also an A. This is the only thing Java knows about what these classes represent or how we intend to use them - Java only knows that every Racer is a Horse and that every Equestrian is also a Horse (and Java knows this only because we said so when we declared the classes). Based on only this information, is it reasonable that Java should allow you to take an Equestrian (which can do stuff all Horses can do, as well as some Equestrian specific things, such as to trot) and start treating it like it was a Racer (which can also do stuff all Horses can do, but can't necessarily trot, and can do some things that Equestrians can't do)?

Also, note that there is a difference between what the compiler will allow and what casts will actually go through when they are attempted. In general, the compiler will allow casts that might succeed. Try each of these code segments; each of them should give a different outcome (one won't compile, one will compile but will throw an exception when it is run, one will compile and run without exceptions). Then, reflect upon why Java is designed to behave in this way.

// Code segment A
Horse h = new Equestrian();
Goat g = (Goat)h;

// Code segment B
Horse h = new Equestrian();
Racer r = (Racer)h;

// Code segment C
Horse h = new Racer();
Racer r = (Racer)h;
只有一腔孤勇 2024-10-26 06:45:12

把它想象成一棵树:

马 -->马术

马--> Racer

Equestrian和Racer是马干马的两个分支。

继承只能从上到下(从主干到分支)进行。子类可以继承父类的方法和变量。因此,Equestrian继承了Horse类的方法和变量。 Racer 还继承了 Horse 类的方法和变量。但是,Racer 或 Equestrian 类的对象不会继承彼此的方法或变量。

以上信息应该足以让您找到答案。完成作业后,这里您可以查找更多信息。尽早尽可能多地阅读此类基本概念非常重要。

Think of this as a tree:

Horse --> Equestrian

Horse --> Racer

Equestrian and Racer are two branches of the trunk Horse.

Inheritance works only from top to bottom (from trunk to branches). Methods and variables of the parent class can be inherited by the sub classes. Therefore, Equestrian inherits the methods and variables of the Horse class. Racer also inherits the methods and variables of the Horse class. BUT, objects of class Racer or Equestrian don't inherit each other's methods or variables.

The above should be enough information for you to figure out the answer. Once you're done with the homework, here's where you can find more information. It is very important to read up on basic concepts such as this as much as you can, early on.

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