这个程序输出什么?

iGoodLoser 2011-12-15 04:22:20

class Father{
public void a(){
System.out.println("father'a()");
}
public void b(){
System.out.println("father'b()");
a();
}
}

class Son extends Father{
public void a(){
System.out.println("son'a()");
super.a();
}
public void b(){
System.out.println("son'b()");
super.b();
}
}

public class Demo{
public static void main(String[] args){
Father son = new Son();
son.b();
}
}
...全文
272 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
iGoodLoser 2011-12-18
  • 打赏
  • 举报
回复
额,我只能说上面回答的基本上都是一知半解!
xlliu12345 2011-12-18
  • 打赏
  • 举报
回复
挺复杂的,看了半天还是没理解
yixiao5211 2011-12-17
  • 打赏
  • 举报
回复
24、25楼正解。典型的多态,主要是看son对象到底是儿子类的还是父类的,一开始认为是子类的,调用了子类的b()方法。
wudi221364 2011-12-17
  • 打赏
  • 举报
回复
这是哪本java初级教程的例题。
其实就是叫你理解继承的概念,
这个程序重在理解。
运行结果不重要。
iGoodLoser 2011-12-16
  • 打赏
  • 举报
回复
这是Thinking in java 中的一句话:“当创建了一个导出类(子类)的对象时,该对象包含了一个基类的子对象。这个子对象与你用基类直接创建的对象是一样的。二者的区别在于,后者来自于外部,而基类的子对象被包装在导出类对象内部。”
我想大家看了这句话应该能明白为什么了,至于更底层更详细的具体细节我想还是以后再说吧!
风雨漂泊人 2011-12-16
  • 打赏
  • 举报
回复
自己跑一下 看输出 就知道怎么调用的了
iGoodLoser 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 louisprogram 的回复:]

Hi all,
This issue is about the difference between override and hidding method.I found some info in oracle's web.[url=http://docs.oracle.com/javase/tutorial/java/IandI/override.html]
I……
[/Quote]
Get it!
Thank you !
  • 打赏
  • 举报
回复
多态问题!你后边NEW出来的是一个SON 的对像,在这个对象里面调用a()时,就应该调用SON对像中的a()方法。
nicklename 2011-12-16
  • 打赏
  • 举报
回复
首先调用Son中的void b() 打出son'b(),然后执行super.b()这样就调用Father中的void b()打出father'b(),然后执行a()这里就涉及到多态了,根据生成的对象调用相应的方法而不是根据引用的类型,这里的son是父类的一个引用,而实际的对象是一个Son对象
yuweng101 2011-12-16
  • 打赏
  • 举报
回复
汗。。。每天都有这种帖子。。。
杀猪剑客 2011-12-16
  • 打赏
  • 举报
回复
多态的概念呗!弄懂了不就会了!
changtianshuiyue 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 louisprogram 的回复:]

Hi all,
This issue is about the difference between override and hidding method.I found some info in oracle's web.[url=http://docs.oracle.com/javase/tutorial/java/IandI/override.html]
I……
[/Quote]
看懂了
CodingBoxes 2011-12-15
  • 打赏
  • 举报
回复
Hi all,
This issue is about the difference between override and hidding method.I found some info in oracle's web.[url=http://docs.oracle.com/javase/tutorial/java/IandI/override.html]
It means that the sun instance class method a() hidding the father class method a().So if you get a instance Son,it just call the method in class Son ,except applying super.
Sorry for my poor english .I just want to practise it. If I have any faults, please contact me.
Thanks.
Flycutter 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 helios_fly 的回复:]

额,我知道他们的调用顺序,但是谁能说出为什么?
是太简单了不屑一顾还是都一知半解??
[/Quote]
一知半解的飘过,所以还请牛人们来回答一下底层的调用原理,这个要涉及到底层的语言存储设计啥的了吧
iGoodLoser 2011-12-15
  • 打赏
  • 举报
回复
额,我知道他们的调用顺序,但是谁能说出为什么?
是太简单了不屑一顾还是都一知半解??
WJL_MGQS 2011-12-15
  • 打赏
  • 举报
回复
class Father{
public void a(){//5
System.out.println("father'a()");
}
public void b(){
System.out.println("father'b()");//3
a();
}
}

class Son extends Father{
public void a(){//4
System.out.println("son'a()");
super.a();
}
public void b(){
System.out.println("son'b()");//2
super.b();
}
}

public class Demo{
public static void main(String[] args){
Father son = new Son();
son.b();//1

}
}
WJL_MGQS 2011-12-15
  • 打赏
  • 举报
回复
class Father{
public void a(){//4
System.out.println("father'a()");
}
public void b(){
System.out.println("father'b()");//3
a();
}
}

class Son extends Father{
public void a(){//3
System.out.println("son'a()");
super.a();
}
public void b(){
System.out.println("son'b()");//2
super.b();
}
}

public class Demo{
public static void main(String[] args){
Father son = new Son();
son.b();//1

}
}
iGoodLoser 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kakagui 的回复:]

class Father{
// step 4
public void a(){
System.out.println("father'a()");
}

// step2
public void b(){
System.out.println("father'b()");

……
[/Quote]
那怎么打印出:
son'b()
father'b()
father'a()
来呢?就是说当调用到super.b()时怎样让它调用Father的a()而不是Son覆盖后的a()呢?
疯狂的驴子 2011-12-15
  • 打赏
  • 举报
回复
貌似,对象是b,a()找到的就是当前对象中的a()方法。
面向对象World 2011-12-15
  • 打赏
  • 举报
回复
错怪kakagui了
包歉 !
加载更多回复(8)

62,616

社区成员

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

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