java-学习10

使用return结束一个方法

public class function3 {
    public static void main(String[] args) {
        System.out.println("1、调用fun()方法之前。");
        fun(10);//调用fun()方法
        System.out.println("2、调用fun()方法之后。");
    }

    private static void fun(int i) {
        // TODO Auto-generated method stub
        System.out.println("3、进入fun()方法。");
        if(i==10) {
            return;//结束方法,返回被调用处
        }
        System.out.println("4、正常执行完fun()方法。");
    }
}

输出:

1、调用fun()方法之前。
3、进入fun()方法。
2、调用fun()方法之后。

猜你喜欢

转载自www.cnblogs.com/liaohongwei/p/9895956.html