avatar
两个看来相似的问题# Programming - 葵花宝典
G*A
1
问题1:
assume that WhiteOnBlackLabel inherits a paint method from JLabel and also
implements its own version of paint.
JLabel label = new WhiteOnBlackLabel("A label");
label.paint(g); //for some Graphics object g
Which of those two implementations of paint will be executed in
label.paint(g); of this example?
paint() defined in label or paint() defined in WhiteOnBlacklabel?
问题2:
All of our Fruits inherit an equals method from class Object. Suppose that
Fruit declares its own equals method:
Object>
avatar
d*d
2
同学,new出来的是个pointer吧。
你把题目改严谨一点先。不然没法回答的。

【在 G****A 的大作中提到】
: 问题1:
: assume that WhiteOnBlackLabel inherits a paint method from JLabel and also
: implements its own version of paint.
: JLabel label = new WhiteOnBlackLabel("A label");
: label.paint(g); //for some Graphics object g
: Which of those two implementations of paint will be executed in
: label.paint(g); of this example?
: paint() defined in label or paint() defined in WhiteOnBlacklabel?
: 问题2:
: All of our Fruits inherit an equals method from class Object. Suppose that

avatar
t*t
3
不是C++吧?

【在 d*******d 的大作中提到】
: 同学,new出来的是个pointer吧。
: 你把题目改严谨一点先。不然没法回答的。

avatar
d*d
4
oh,看上去会不会是java?

【在 t****t 的大作中提到】
: 不是C++吧?
avatar
R*Z
5
赫赫, 都看见JLabel了, 当然是Java

【在 d*******d 的大作中提到】
: oh,看上去会不会是java?
avatar
R*Z
6
i believe #1 should be WhiteOnBlacklabel's paint(), and #2 should call Fruit
's equalTo()
Using C++ terminology, all Java functions/methods are virtual (unless it's
declared final I guess). So if you have a class D inherited from base class
B, even if you refer to this object as B, it is acutally a D object and
will call D's methods.

【在 G****A 的大作中提到】
: 问题1:
: assume that WhiteOnBlackLabel inherits a paint method from JLabel and also
: implements its own version of paint.
: JLabel label = new WhiteOnBlackLabel("A label");
: label.paint(g); //for some Graphics object g
: Which of those two implementations of paint will be executed in
: label.paint(g); of this example?
: paint() defined in label or paint() defined in WhiteOnBlacklabel?
: 问题2:
: All of our Fruits inherit an equals method from class Object. Suppose that

avatar
g*g
7
It's called abstract in java terms. What you say holds though.

Fruit
class

【在 R***Z 的大作中提到】
: i believe #1 should be WhiteOnBlacklabel's paint(), and #2 should call Fruit
: 's equalTo()
: Using C++ terminology, all Java functions/methods are virtual (unless it's
: declared final I guess). So if you have a class D inherited from base class
: B, even if you refer to this object as B, it is acutally a D object and
: will call D's methods.

avatar
R*Z
8
i thought Java's abstract methods are like pure virtual functions in C++,
and Java's abstract classes are like classes that contain pure virtual
functions in C++.

【在 g*****g 的大作中提到】
: It's called abstract in java terms. What you say holds though.
:
: Fruit
: class

avatar
j*g
9
I think the two questions maybe not the same.
For question one, if the subclass' paint is using Graphics object as its
parameter, it's an overwriting. The runtime execution will call subclass'
paint method.
For the second question, two methods have different signatures. That's
overloading. Which method is called depends on excact parameter matching. In
this case, the Fruit one should be called.
avatar
G*A
10
第二个问题正确答案是Object.

Fruit
class

【在 R***Z 的大作中提到】
: i believe #1 should be WhiteOnBlacklabel's paint(), and #2 should call Fruit
: 's equalTo()
: Using C++ terminology, all Java functions/methods are virtual (unless it's
: declared final I guess). So if you have a class D inherited from base class
: B, even if you refer to this object as B, it is acutally a D object and
: will call D's methods.

avatar
G*A
11
You got the point! For the second one, these is NO overrrding because two
menthods have different signatures.

In

【在 j****g 的大作中提到】
: I think the two questions maybe not the same.
: For question one, if the subclass' paint is using Graphics object as its
: parameter, it's an overwriting. The runtime execution will call subclass'
: paint method.
: For the second question, two methods have different signatures. That's
: overloading. Which method is called depends on excact parameter matching. In
: this case, the Fruit one should be called.

avatar
c*x
12
the purpose of two questions are the same, they test what
polymorphysm,
1. label.paint(g); // WhiteOnBlackLabel.paint() is called.
2. fo.equals(f) // will actually call public boolean equals(Fruit fruit)
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。