public class work2 {
public static void main(String[] args){
father A=new father();
son C=new son();
System.out.println("A="+C.f());
}
}
class father{
int i=1;
public int f(){
if(i==4)
return 0;
System.out.println(i+++"father");
return f();
}
}
class son extends father{
static int j=1;
public int f(){
System.out.println(j+++"son="+super.f());
return 0;
}
}
public static void main(String[] args){
father A=new father();
son C=new son();
System.out.println("A="+C.f());
}
}
class father{
int i=1;
public int f(){
if(i==4)
return 0;
System.out.println(i+++"father");
return f();
}
}
class son extends father{
static int j=1;
public int f(){
System.out.println(j+++"son="+super.f());
return 0;
}
}
0
0
2father
3father
4son=0
3son=0
2son=0
1son=0
A=0
这输出结果 ~~ 有可能困惑在于 father 调用的 f() 是son 里面的 ~~
public static void main(String[] args){
// father A=new father(); delete
son C=new son();
System.out.println("A="+C.f());
}
}
class father{
int i=1;
public int f(){
if(i==4)
return 0;
System.out.println(i+++"father");
return f();
}
}
class son extends father{
static int j=1;
public int f(){
System.out.println(j+++"son="+super.f());
return 0;
}
}
public class work2 {
public static void main(String[] args){
// father A=new father(); delete
son C=new son();
System.out.println("A="+C.f());
}
}
class father{
int i=1;
public int f(){
if(i==4)
return 0;
System.out.println(i+++"father");
return f();//这里做递归调用 f()调用的 是son里面的 f()
}
}
class son extends father{
static int j=1;
public int f(){
System.out.println(j+++"son="+super.f());
return 0;
}
}
为什么不是这结果:
1father
2father
3father
4son=0
A=0
1father
2father
3father
1son=0
A=0