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();
}
}
father'b()
son'a()
father'a()
应该是这样的吧
father'b()
son'a()
father'a()
为什么当调用了super.b()后,会调用子类的a(),而不是父类的a()?
// step 4
public void a(){
System.out.println("father'a()");
}
// step2
public void b(){
System.out.println("father'b()");
// since instance of son --> son.a();
a();
}
}class Son extends Father{
// step3
public void a(){
System.out.println("son'a()");
// a() of father
super.a();
}
// step1
public void b(){
System.out.println("son'b()");
// b() of father
super.b();
}
}public class Demo{
public static void main(String[] args){
Father son = new Son();
son.b();
}
}稍微解释了一下。;-)
包歉 !
son'b()
father'b()
father'a()
来呢?就是说当调用到super.b()时怎样让它调用Father的a()而不是Son覆盖后的a()呢?
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 }
}
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 }
}
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.
Thank you !
我想大家看了这句话应该能明白为什么了,至于更底层更详细的具体细节我想还是以后再说吧!
其实就是叫你理解继承的概念,
这个程序重在理解。
运行结果不重要。