求java大神 类的继承问题

public class Demo{
public static void main(String args[])
{ Sub sb = new Sub( );
System.out.println(sb.method1( ));

}
}
class Super{
int x=1 , y=2;
public int method1(){
return ((x<y)?x:y);
}
}
class Sub extends Super{
public int mothod1( )
{
return ((x>y)?x:y);
}
}
结果是什么?为什么会得到这个结果?

//看注释吧!
public class Demo {
public static void main(String args[]) {
Sub sb = new Sub();
System.out.println(sb.method1());

}
}

class Super {
int x = 1, y = 2;
public int method1() {
return ((x < y) ? x : y);

//return返回一个int值,这个值是多少呢?

//x小于Y吗?真(是的那就返回x):假(不是的那就返回y);

//这个结果,x是小于1的,那么就是真,就会返回x值了!
}
}

class Sub extends Super {

//当发生继承以后,这个Sub类,
//等于也有int x = 1, y = 2;

public int mothod1() {
//这里完全可以直接这么写,直接调用了父类mothod1方法!
return super.method1();
}
}

追问

朋友,你也被这道题坑了,父类那个方法跟子类的是不一样的,我当时以为两个一样。。。

追答

哈哈,是的,真是醉了,文字游戏啊!

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答