易错java笔试题

1

public class Test1 extends Test2 {

    public  String name = "test1";//或者private

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        System.out.println(test1.getName());
    }

}

class Test2 {
    public  String name = "test2";//或者private

    public String getName() {
        return name;
    }
}
public class Test1 extends Test2 {

    public  String name = "test1";

    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        System.out.println(test1.getName());
    }

}

class Test2 {
    public  String name = "test2";

    public String getName() {
        return name;
    }
}

2

public class Test {

    public int div(int a, int b) {

        try {

            return a / b;

        } catch (Exception e) {

            System.out.println("Exception");

        } catch (NullPointerException e) {

            System.out.println("ArithmeticException");

        } catch (ArithmeticException e) {

            System.out.println("ArithmeticException");

        } finally {

            System.out.println("finally");

        }

        return 0;

    }

    public static void main(String[] args) {

        Test demo = new Test();

        System.out.println("商是:" + demo.div(9, 0));
    }
}

Excepton ‘java.lang.NullpointException’ has already been caught
捕获异常时,首先捕获范围小的异常

       catch (NullPointerException e) {

            System.out.println("ArithmeticException");

        } catch (ArithmeticException e) {

            System.out.println("ArithmeticException");

        } catch (Exception e) {

            System.out.println("Exception");

        } finally {

            System.out.println("finally");

        }

猜你喜欢

转载自blog.csdn.net/peng_hong_fu/article/details/74034821