java毕向东学习笔记——day09

来源:互联网 发布:pace.js 在线演示 编辑:程序博客网 时间:2024/06/05 09:25

java毕向东学习笔记——day09

内部类

  1. 外部类要访问内部类,必需先建立内部类的对象;
  2. 内部类可以直接访问外部类;
class Outer{    int x = 1;    class Inner{                                  int x = 2;        void function(){            int x = 3;            System.out.println(x);            System.out.println(this.x);            System.out.println(Outer.this.x);           }        void show(){            System.out.println("在主函数中成功地创建了内部类的对象!");        }    }    void method(){        Inner in = new Inner();        in.function();    }}class InnerDemo{    public static void main(String[] args){        Outer ot = new Outer();        ot.method();        System.out.println("------------------------------------");        Outer.Inner in = new Outer().new Inner();        in.show();        System.out.println("------------------------------------");    }}

第二版

  1. 内部类处于成员的位置,因此可以被private和static修饰
class Outer{    static class Inner{                              // 内部类处于成员的位置,因此可以被private和static修饰        void show1(){            System.out.println("在外部其他类中直接访问内部类的非静态方法");        }        static void show2(){            System.out.println("在外部其他类中直接访问内部类的静态方法");        }    }}class InnerDemo{    public static void main(String[] args){        System.out.println("------------------------------------");        new Outer.Inner().show1();              // 直接访问内部类的非静态方法        Outer.Inner.show2();        // 静态方法可直接由类名访问    }}

内部类不处于成员位置时

class Outer1{    int x = 2;    void method(final int a){             final int y = 3;                  //若局部变量不为常量,则无法被局部内部类访问        class Inner1{                     //局部内部类不处于成员位置(1),无法被static修饰,因此也无法在其中定义static方法        //因为对于内部类,存在静态方法时,类也要求为静态,这就与(1)相矛盾             void show(){                System.out.println("局部的内部类");                System.out.println(x);                System.out.println(y);            }            void show2(){                System.out.println(a);            }        }            new Inner1().show();            new Inner1().show2();    }}class InnerDemo1{    public static void main(String[] args){        Outer1 o = new Outer1();        o.method(4);        //method1执行完毕        o.method(5);        //出栈后执行method2    }}

匿名内部类

  1. 匿名内部类建立的前提是要继承一个类或者实现接口;
  2. 匿名内部类其实就是内部类的简写;
  3. 匿名内部类的常见使用情景:
    Interface Inter{
    public abstract void method();
    }

    主函数{
    show();
    }

    public static void show(Inter in){
    in.method();
    }
    如上,要传进show方法的参数是一个Inter类型的对象,一般情况是新建一个类实现Inter接口,而有了匿名内部类之后,可以采用如下方法:

    show(new Inter(){    void method(){}});

/**    匿名内部类*/abstract class CeShi{    abstract void show();}class Outer{    int x = 3;      void function(){        CeShi s = new CeShi(){                       //父类引用指向子类对象            public void show(){                System.out.println("x====="+x);            }        };        s.show();        //调用了匿名内部类的方法    }}class NiMinDemo{    public static void main(String[] args){        new Outer().function();    }}

异常的抛出,捕获和处理

class Divide{    int div (int a, int b) throws ArithmeticException,ArrayIndexOutOfBoundsException{        int[] arr = new int[a];        int c = arr[3];        return a/b;    }}class ExceptionDemo{    public static void main(String[] args){        Divide d = new Divide();        try{            int x = d.div(3,1);     //可修改为(4,0)测试除零异常            System.out.println(x);        }        catch(ArithmeticException e){            System.out.println("除零异常");            System.out.println(e.toString());        }        catch(ArrayIndexOutOfBoundsException e){            System.out.println("数组越界");            System.out.println(e.toString());        }    }}

自定义异常

class Divide{    int div (int a, int b) throws ArithmeticException,ZiDindYiException{        if ((a/b)<0)            throw new ZiDindYiException("假装负数也是异常");        return a/b;    }}class ZiDindYiException extends Exception{    //自定义异常类    ZiDindYiException(String Message){        super(Message);    }}class ExceptionDemo{    public static void main(String[] args){        Divide d = new Divide();        int x = 0;        try{            x = d.div(3,-1);        }        catch(ArithmeticException e){            System.out.println("算术异常");            System.out.println(e.toString());        }        catch(ZiDindYiException e){            System.out.println("自定义异常");            System.out.println(e.toString());        }        System.out.println(x);    }}

运行时异常

  1. 异常分两种:一种是运行时异常(RuntimeException),这种异常编译可过,但运行时会导致程序停止,需要程序员修改代码;一种是非运行时异常,编译都过不了,需要调用者自己调试。
  2. 常见的RuntimeException有ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException;
  3. 运行时异常可在方法中throw而不在方法外throws,程序会编译通过,并且即使在方法外throws而在主程序中未抛出或者catch,程序一样会编译通过;
  4. 因此在自定义异常时,应当考虑其继承的是RuntimeException还是Exception;

异常练习

/**    异常的练习*/class Teacher{    private String name;    private Computer pc;    Teacher(String name){        this.name = name;    }    void teach() throws NoClassException{        pc = new Computer();        try{            pc.useTeach();        }        catch(BlueException e){            pc.reset();        }        catch(CigaratteException e){            throw new NoClassException(name+"没法讲课咯");        }    }}class Computer{    int state = 3;             //代表电脑工作状态,1正常工作,2蓝屏,3冒烟了    void useTeach()throws BlueException,CigaratteException{        if (state == 1)            System.out.println("电脑可以被用来讲课");        else if (state == 2)            throw new BlueException("电脑蓝屏了");        else if (state == 3)            throw new CigaratteException("电脑冒烟啦");        else             System.out.println("不存在的!");    }    void reset(){        if (state == 2)            state = 1;        System.out.println("电脑重启");    }}class BlueException extends Exception{    BlueException(String message){        super(message);    }}class CigaratteException extends Exception{    CigaratteException(String message){        super(message);    }}class NoClassException extends Exception{    NoClassException(String message){        super(message);    }}class ExceptionTest{    public static void main(String[] args){        Teacher t = new Teacher("毕老师");        try{            t.teach();        }        catch(NoClassException e){            System.out.println("放假放假!!");        }    }}