无意中看到下面一个题目,大家一起来看看最后的输出结果是什么。反正我看完之后,用IDE测试后感觉知识点得到巩固了。
1 /** 2 * 函数执行顺序测试 3 * Created by 萌小Q on 2017/5/17 0017. 4 */ 5 public class ExeSeqTest { 6 7 public static void main(String [] args){ 8 System.out.println(new B().getValue()); 9 } 10 static class A{ 11 protected int value; 12 public A(int v) { 13 setValue(v); 14 } 15 public void setValue(int value){ 16 this.value = value; 17 } 18 public int getValue(){ 19 try{ 20 value++; 21 return value; 22 } catch(Exception e){ 23 System.out.println(e.toString()); 24 } finally { 25 this.setValue(value); 26 System.out.println(value); 27 } 28 return value; 29 } 30 } 31 static class B extends A{ 32 public B() { 33 super(5); 34 setValue(getValue() - 3); 35 } 36 public void setValue(int value){ 37 super.setValue(2 * value); 38 } 39 } 40 }
执行结果:
22 34 17