你知道输出什么吗?

nsdcomputer 2009-08-16 09:14:19
刚刚在看《深入浅出java虚拟机》的时候,发现了一个比较不错的题目,分享一下:

class Superclass{

public Superclass(){
interestingMehod();
}

void exampleMethod(){
interestingMehod();
}

private void interestingMehod(){ //注释
System.out.println(
"Superclass's interesting method.");
}


}

class Subclass extends Superclass{
public Subclass(){
interestingMehod();
}

public void interestingMehod(){
System.out.println(
"Subclass' interesting method.");
}

}
//Test.java
public class Test{
public static void main(String[] args){
Subclass me = new Subclass();
me.exampleMethod();
}
}

上面的代码输出什么呢?然后把“注释”处的函数的private改为protected,public或直接去掉,又怎么样呢?
...全文
224 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
小猪头是猪 2009-08-18
  • 打赏
  • 举报
回复
正确结果是:
Superclass's interesting method.
Subclass' interesting method.
Superclass's interesting method.

分析:
创建一个对象并对其实例化
Subclass me = new Subclass();
实例化的过程是:执行构造函数
public Subclass(){
interestingMehod();
}

因为构造函数继承Superclass类
所以调用Superclass中interestingMehod();方法
然后执行构造函数第二个方法
public void interestingMehod(){
System.out.println(
"Subclass' interesting method.");
最后使用对象。方法执行方法
要是弄懂这个问题最好是去C++的继承 你就明白了

GatoAngel 2009-08-18
  • 打赏
  • 举报
回复
学习中。加油!
Miracle1216 2009-08-18
  • 打赏
  • 举报
回复
不该的话是:
Superclass's interesting method.
Subclass' interesting method.
Superclass's interesting method.
改过之后全部是:
Subclass' interesting method.

不改:
1. 调用之类构造函数前要调用父类的构造函数,所以先调用父类的interestingMehod(),
然后调用子类的interestingMehod(),然后再调用父类的exampleMethod()。
24K純帥 2009-08-18
  • 打赏
  • 举报
回复
Superclass's interesting method.
Subclass' interesting method.
Superclass's interesting method.
ddyouyue 2009-08-18
  • 打赏
  • 举报
回复
Superclass's interesting method.
Subclass' interesting method.
Superclass's interesting method.
catici 2009-08-18
  • 打赏
  • 举报
回复
俺是来学习的~~~
lyu880606 2009-08-18
  • 打赏
  • 举报
回复
Superclass's interesting method.
Subclass' interesting method.
Superclass's interesting method.

说明: 在任何情况下, 如果父类有无参的构造函数, 那么先运行父类的构造函数,再运行子类的构造函数
priestmoon 2009-08-18
  • 打赏
  • 举报
回复
mark
mianfeidog 2009-08-17
  • 打赏
  • 举报
回复
长见识了。
wwfsfe 2009-08-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 problc 的回复:]
"Superclass's interesting method."
不运行程序的话,觉得是这个结果
[/Quote]


zheng jia renqi
luck_dream 2009-08-17
  • 打赏
  • 举报
回复
正解
[Quote=引用 2 楼 viszl 的回复:]
不作更改感觉输出是
"Superclass's interesting method.
"Subclass' interesting method.
"Superclass's interesting method.
[/Quote]
lulu0126 2009-08-17
  • 打赏
  • 举报
回复
谢谢 楼主!

这个代码很有代表性~

学习了!
liming861105 2009-08-17
  • 打赏
  • 举报
回复
遇到过好几次这样类似的问题,有没高手能给说明其中缘由
  • 打赏
  • 举报
回复
晕,我也没看清那个private
chenchengamao 2009-08-17
  • 打赏
  • 举报
回复
在多态中,如果A方法调用B方法,如果B方法是private的,那么调用的B方法是和A方法在同一个类中的B方法。
nayiye 2009-08-17
  • 打赏
  • 举报
回复

输出都是
Superclass's interesting method.
Subclass' interesting method.
Superclass's interesting method.
lovecj6185 2009-08-17
  • 打赏
  • 举报
回复
呵呵,知道了
没有看清楚private关键字,还以为方法被重写了呢
hzq237 2009-08-17
  • 打赏
  • 举报
回复
不更改 我觉得是
"Superclass's interesting method".
"Subclass' interesting method."
"Superclass's interesting method".
更改后,我觉得是
"Subclass' interesting method."
"Subclass' interesting method."
"Subclass' interesting method."





qiubojuncode 2009-08-17
  • 打赏
  • 举报
回复
运行结果和想的不一样
starwing1981 2009-08-16
  • 打赏
  • 举报
回复
改为protected,public或直接去掉
就是重写父类方法了
Subclass' interesting method.
Subclass' interesting method.
Subclass' interesting method.
加载更多回复(3)

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧