A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© crazylong 中级黑马   /  2014-6-5 20:30  /  1251 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用代码证明,在try中写了return,后面又写了finally,是先执行return还是先执行final

3 个回复

倒序浏览
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test3 {
  public static void main(String[] args) {
                  new Test3().show();   //调用下面的方法
  }
  
  
  public void show(){
         
          File file= new File("D:\\sd.txt"); //找地址
         
          FileReader reader=null;
          try {                                                       
                 reader =new FileReader(file);

                  System.out.println(reader);
                  
                  return ;
                  //System.out.println(4);  编译会报错
        } catch (FileNotFoundException e) {
               
                e.printStackTrace();
        }finally{
                try {
                        reader.close();
                } catch (IOException e) {
               
                        e.printStackTrace();
                }
        }
          
  }
}

先执行try的return
回复 使用道具 举报
/**
* 第七题:分析运行结果,说明原理
* @author Administrator
*
*/
public class Test7 {
         /*class A {
         void fun1() {
               System.out.println(fun2());
         }
         int fun2() {
             return 123;
         }
     }
      public class B extends A {
         int fun2() {
             return 456;
      }
      public static void main(String args[]) {
         B b = new B();
         b.fun1();
         A a = b;
         a.fun1();
      }
    }*/
       
        /*
         * 分析:class B继承了class A,就拥有了A中的方法,fun2()方法被重写,当B  b = new B()创建子类对象,子类对象b调用fun1()也就是父类的方法fun1(),
         * 父类中的方法又调用了子类的fun2()方法,所以打印456。
         * 父类A  a=b 这是多态,父类引用指向了子类对象 ,a调用fun1()方法,调用的就是父类中的fun1(),父类引用指向子类对象时,如果父类的引用调用的方法没有被子类重写,
         * 就会调用父类中的方法。如果子类中重写了父类的方法,就会调用子类重写的方法 ,所以打印456*/
}
回复 使用道具 举报
package com.itheima;

/**
* 第五道題:用代码证明,在try中写了return,后面又写了finally,是先执行return还是先执行fianlly?
* @author Administrator
*
*/
public class Test5 {
        /**
         * 定义一个方法 输出字符串
         * @return 1 表示是try里的方法
         */
        public static int getTryReturn(){
                System.out.println("我是try里的return,我执行了");
                return 1;
        }
       
        /**
         * 定义一个方法 输出字符串
         * @return 2  表示是finally里的方法
         */
        public static int getFinallyReturn(){
                System.out.println("我是finallly里的return,我执行了");
                return 2;
        }
        /**
         * 定义方法 ,测试是先执行return还是先执行fianlly
         * @return 正整数
         */
        public static int test(){
                try {
                        return getTryReturn();
                } catch (Exception e) {
                        e.printStackTrace();
                }finally{
                        return getFinallyReturn();
                }
        }
        /**
         * 测试类
         * @param args
         */
        public static void main(String[] args) {
                System.out.println(test());
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马