java IS-A关系考试题困惑

发布于 2024-09-09 02:45:14 字数 427 浏览 1 评论 0原文

来自大师考试:

哪些说法是正确的? (选择全部 适用)

<代码>A。 is-一种关系总是依赖 继承
B。 is-一种关系总是依赖 实例变量
C。 is-a 关系总是需要 at 至少两个类类型
D。 is-一种关系总是依赖 多态性
E。是-关系总是 紧密耦合

正确答案:A、C、D

我不明白 A、C 或 D 中的任何一个是如何正确的。

一个对象对象就是一个对象。字符串对象是字符串。这些语句中的每一个都只有一种类类型,并且不应用继承或多态性。

是我的理由错误还是答案不正确?

From MasterExam:

Which statements are true? (Choose all
that apply)

A. is-a relationship always rely on
inheritance
B. is-a relationship always rely on
instance variables
C. is-a relationship always require at
least two class types
D. is-a relationship always rely on
polymorphism
E. is-a relationship are always
tightly coupled

Correct answers: A, C, D

I don't see how any of A, C or D are correct.

An Object object IS-A Object. A String object IS-A String. There is only one class type in each of these statements and no inheritance or polymorphism applies.

Is my rationale wrong or are the answers incorrect?

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

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

发布评论

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

评论(6

妄断弥空 2024-09-16 02:45:14

“关系”是指两个类之间的关系。 is-a 关系是一种使用继承的关系(与使用组合的 has-a 相对)。例如,字符串是对象。类不能从自身继承,这意味着 C。附带说明,类可以与自身进行组合 (has-a)。例如,一个人可以有另一个人作为母字段。

只要有继承,子类的实例就可以用作超类的实例。这就是多态性,也就是说D也是对的。

"Relationship" refers to a relationship between two classes. An is-a relationship is a type of relationship that uses inheritance (as opposed to e.g. has-a, which uses composition). For instance, a String is-a Object. A class can't inherit from itself, which implies C. As a side note, a class could have composition (has-a) with itself. E.g. a Person could have another Person as a mother field.

Any time you have inheritance, an instance of the subclass can be used as an instance of the superclass. That's polymorphism, which means D is also right.

请叫√我孤独 2024-09-16 02:45:14

A. 字符串就是字符串……这是一个同义反复,被认为是显而易见的。但查看 API 文档,您会发现 java.lang.String 也是 java.lang.Object。事实上,所有Java类都继承自java.lang.Object。它们继承了 Object 的基本属性,并添加了一些自己的其他属性。这就是“是”关系的全部内容。

C. 同样,禅宗佛教徒对任何对象本身的同义反复感兴趣,但对语言设计师和其他计算机科学家不感兴趣。因此,要建立“is-a”关系,您需要两个不同的类。

D.我不太确定。如果有人问我,我会说多态性取决于“is-a”关系,而不是相反。

A. That a String is a String is... a tautology, considered obvious. But looking at the API documentation, you find that a java.lang.String is also a java.lang.Object. In fact, all Java classes inherit from java.lang.Object. They inherit basic properties of Object and add some others of their own. That's what the is-a relationship is all about.

C. Again, the tautology about any object being itself is of interest to Zen Buddhists but not language designers and other Computer Scientists. So to have an "is-a" relationship, you need two distinct classes.

D. I'm not so sure here. If I were asked, I'd say polymorphism depends on the "is-a" relationship, not the other way around.

蝶舞 2024-09-16 02:45:14

您的理由略有偏差,因为这种关系适用于,而不是对象。

字符串是一个对象,因为 String 继承自 Object。类似地,FileOutputStream IS-A OutputStream IS-A Object

IS-A是类与类之间的关系,而不是类与对象之间的关系。

Your rationale is slightly off, as this relationship applies to classes, not to objects.

A string IS-A object since String inherits from Object. Similarly a FileOutputStream IS-A OutputStream IS-A Object.

IS-A is a relationship between classes, no between classes and objects.

枫以 2024-09-16 02:45:14

你需要看到它有点不同:

  • 是一个动物
  • 汽车有一个引擎
  • 一个字符串是一个对象
  • 一个字符串has-a char array

尝试这样看,现在应该变得更清楚了。

You need to see it a bit different:

  • A cat is-a animal
  • A car has-a engine
  • A String is-a object
  • A String has-a char array

Try to see it that way, it should become clearer now.

寒冷纷飞旳雪 2024-09-16 02:45:14

我认为A点和A点C 现在已经很清楚了...关于 D.虽然,技术上可能可以扩展一个没有实例行为或属性的类..(空白或可能仅具有静态元素)...但这原则上有点失败“扩展”类的目的是因为您想要从继承树上更高的类继承行为/功能。
所以,我想说选项 D. 是正确的(几乎):)(如果我们不太技术性的话)。顺便说一句,这可能是一个有趣的讨论。

I think points A & C are pretty clear by now... regarding D.though ,it may be technically possible to extend a class with no instance behavior or property..(blank or maybe only with static elements) ...but that in principle kind of defeats the purpose of "extending" classes as you want to inherit behavior/functionality from classes higher up the inheritance tree.
So,i would say option D. is correct(almost) :) (if we are not going too technical).btw this can be an interesting discussion.

情释 2024-09-16 02:45:14

抱歉,你的理性是错误的。他们正在谈论两个不同的班级。例如,字符串是一个对象。

Sorry, your rational is wrong. They are talking about two different classes. For example a String is-a Object.

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