名人名言
“A successful man is one who can lay a firm foundation with the bricks others have thrown at him.” ——David Brinkley
成功的人能够把别人扔向他的板砖成为他的根基。
——戴维·布林克利
“Those who dare to fail miserably can achieve greatly.” ——John F. Kennedy
2019.04.09问题及解析
public class Son extends Father{
public static void main(String[] args) {
new Son().run();//第三行
}
private final void run(){//第五行
System.out.println("son");
}
}
class Father{
private final void run(){
System.out.println("father");
}
}
请问结果输出什么?
A.son
B.father
C.第三行编译错误
D.第五行编译错误
E.抛出运行异常
final修饰的方法不可以被重写,如果子类对final修饰的方法进行重写则编译报错。
private修饰的方法对于子类时不可见的,同样的方法名在父类和子类中同时出现表示的是新定义的方法,与父类无关。
子类父类存在相同方法时,子类进行调用时,优先调用子类的方法。
定义了一个Son类继承于Father类
首先new了一个Son对象,并且调用了它的run()方法
定义了一个由一个private、final修饰的run方法,输出son
定义了Father类,同样有一个private、final修饰的run方法,输出father
因为父类的run方法由private、final修饰因此与子类无关,如果去除private则子类run()编译错误,因此本身代码不存在问题。
将会直接调用子类的run方法输出son,答案选A。
2019.04.10问题
public class ExceptionTest {
public static void main(String[] args) {
int number = 0;
System.out.println(returnTest(number));
}
private static int returnTest(int number){
try{
return number++;
}catch (Exception ignored){
}finally {
return ++number;
}
}
}
请问结果输出什么?
A.0
B.1
C.2
D.编译错误
E.输出异常
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有