现在的位置: 首页 > 综合 > 正文

java基础_Day09

2017年09月30日 ⁄ 综合 ⁄ 共 6475字 ⁄ 字号 评论关闭

1,内部类(内置类,嵌套类):将一个类定义在另一个类的里边。

2,内部类与外部类之间的访问特点:
     A.内部类可以直接访问外部类中的成员,包括私有成员。
     B.外部类需要建立内部类的对象,才可以访问内部类中的成员。
     C.eg1:

class Demo

{

     private static int x = 3;

     static class Demo1//在成员位置上时可以被private/static修饰,被static修饰后只能访问外部类中的static成员

     {

          //int x = 8;

          static void show1()//当内部类中定义了static成员时,该类必须是static类,当外部类中的static成员访问内部类时,内部类也必须是static的

          //void show1()

          {

               //int x = 10;

               System.out.println("inner: "+x);//x取值为最小范围内的取值。

               //System.out.println("inner: "+this.x);//x取值内部类中的取值。

               //System.out.println("inner: "+Demo.this.x);//x取值外部类中的取值。

          }

     }

     void show2()

     {

          Demo1 d = new Demo1();//外部类建立对象访问内部类中的成员

          d.show1();

     }

}

class  NeiBuLei

{

     public static void main(String[] args)

     {

          Demo.Demo1.show1();//在外部其他类中访问访问static内部类中的static成员。

          //new Demo.Demo1().show1();//在外部其他类中访问static修饰的内部类中的非static成员。

          //Demo de = new Demo();

          //de.show2();

          //Demo.Demo1 in = new Demo().new Demo1();//外部其他类直接访问内部类对象。

          //in.show();

     }

}

//啥时候用内部类?

     //当描述事物时候,事物的内部还有事物,该内部事物用内部类来描述。

       eg2:

class Demo

{

     //int x = 10;

     void show(final int x)

     { //x++;//不能指定最终参数 x

          class Demo1//当内部类定义在局部时,类+类中的成员都不可以被成员修饰符修饰;可以访问外部类中的成员,因为还持有外部类的引用;

          //但是不可以访问它所在的局部中的变量,只能访问被final所修饰的局部变量。

          {

               void function()

               {

                    System.out.println("x=="+x);

               }

          }

          new Demo1().function();

     }

}

class NeiBuLei2

{

     public static void main(String[] args)

     {

          new Demo().show(2);

          new Demo().show(34);

     }

}

3,匿名内部类:

     A,是内部类的简写格式,定义类的前提是继承类或实现接口
     B,格式:new 父类或者接口(){};
     C,其实匿名内部类就是一个匿名子类对象.定义的方法一般不超过3个
     D,eg1:

interface Inner

{

     void method();

}

class Test

{

     public static Inner function()

     {

          return new Inner()

          {

               public void method()

               {

                    System.out.println("内部类");

               }

               void show(){}

          };

     }

}

class NeiBuLei3

{

     public static void main(String[] args)

     {

          Test.function().method();

//          Test.function():Test类中有一个static的function方法

//          .method():function方法返回值为一个对象,而且是一个Inner类型的对象

     }

}

4,异常:

     A.是程序运行时出现的不正常的现象。
     B.划分/异常体系:
        Throwable:
---error:对于严重的问题,一般不编写针对性的方式进行处理,如OutOfMemoryError;
---Exception:编写代码进行处理。
     C.try-catch:
          try{需要被检测的代码} 
          catch(异常类 变量){处理代码}
          finally{一定要执行的代码}
              
     D.异常处理过程:
          eg:

class Demo

{

     int show(int a,int b)

     {

          return a/b;//引发问题,java将其封装为对象:new AritchmeticException(),将问题丢给调用者(主函数)

     }

}

class ExceptionDemo1

{

     public static void main(String[] args)

     {

         

          try

          {

               int x = new Demo().show(3,0);//检测到了问题:new AritchmeticException(),如果没有检测代码,主函数没办法处理,将问题丢给虚拟机,虚拟机调用默认的异常处理机制将其处理。如果没有检测到问题,则不执行catch语句;

               System.out.println(x);//这句代码不会执行

          }

          catch (Exception e)//catch接收:Exception e = new AritchmeticExcetion()

          {

               System.out.println("除数不能为0");

//               System.out.println(e.getMessage());//异常信息

//               System.out.println(e.toString());//异常名称:异常信息

               e.printStackTrace();//异常名称:异常信息 异常出现的位置

          }

          System.out.println("OVER");    

     }

}

       E.throwable常用的方法:

          String getMessage();//返回:异常名称:异常信息
          String toString();//返回:异常信息
          String printstacktrace();//返回:异常名称 异常信息 异常出现的位置 
       F.throws:异常声明
          eg:

class Demo

{

     int show(int a,int b)throws Exception//通过throws关键字在该功能上声明可能会出现问题

     {

          return a/b;

     }

}

class ExceptionDemo2

{

     public static void main(String[] args)//throws Exception

     {

//          int x =new Demo().show(4,0);

//          System.out.println(x);//OVER不执行,因为出现问题,程序停止。         

          try

          {

               int x =new Demo().show(4,0);

               System.out.println(x);                   

          }

          catch (Exception e)

          {

               System.out.println(e.toString());

          }//OVER执行。

          System.out.println("OVER");

     }

}

G:多异常处理:
     1,建议声明更为具体的异常,这样可以更具体的处理     

     2,对方声明了几个异常,就有几个对应的catch块,不要定义多余的catch块,如果出现父类的catch块时,要将其放到最下面。

     3,进行catch处理时,catch中一定要定义更为具体的处理方式,不要简单定义一句printStackTrace(),或者简单的一句输出语句,要定义具体的处理方式:比如生成日志文件。

     eg:

class Demo1

{

     int show(int a,int b)

     {

          int[] arr = new int[a];

          return (arr[a])/b;//两个异常不可能同时出现。

     }

}

class ExceptionDemo3

{

     public static void main(String[] args)throws  ArithmeticException,ArrayIndexOutOfBoundsException

     {

          try

          {

               int x = new Demo1().show(4,0);

               System.out.println(x);

          }

          catch (ArithmeticException e)

          {

               System.out.println("除数不能为0");

          }

          catch(ArrayIndexOutOfBoundsException e)

          {

               System.out.println("角标越界了");

          }

          System.out.println("Hello World!");

//          catch(Exception e)

//          {

//               System.out.println(e.toString());

//          }//放到最后,否则读取不到其他处理语句。

     }

}

H:自定义异常:

 1, 对java中特有的问题进行描述,并封装对象。   
 2,在函数内部出现了throw抛出异常对象,要么在内部try catch处理,要么函数上声明让调用者处理。

 3,如何自定义异常信息

          因为父类中已经完成了定义异常信息的操作。所以子类只需要在构造函数时将异常信息传递给父类通过super语句,然后通过getMessage方法或者自定义异常信息。
          自定义异常必须继承Exception
           继承Exception原因:异常体系的一个特点,异常类和异常对象都可抛出。可抛性是Throwable体系中独有的特点,只有这个体系中的类和对象才可以被throws和throw操作。
4,eg:    

class FushuException extends Exception

{

//     private String msg;

//     FushuException(String msg)

//     {

//          this.msg=msg;

//     }

//

//     public String getMessage()

//     {

//          return msg;

//     }

    

     private int value;

     FushuException(String msg,int value)

     {

          super(msg);

          this.value=value;

     }

     public int getValue()

     {

          return value;

     }

}

class Demo4

{

     int show(int a,int b)throws FushuException

     {

          if(b<0)

               throw new FushuException("出现了除数是负数的情况 /by fushu",b);

          return a/b;

     }

}

class ExceptionDemo4

{

     public static void main(String[] args)throws FushuException

     {

          try

          {

               int x = new Demo4().show(3,-2);

               System.out.println(x);

          }

          catch (FushuException e)

          {

               System.out.println(e.toString());                

               System.out.println("除数不能为负数");

               System.out.println("出现的负数是:"+e.getValue());

          }

          System.out.println("Hello World!");

     }

}

I:throws与throw的区别:

    throws用在函数上,在()与{}之间,后面加异常类,可以加多个,用“,”隔开;throw用在函数内,后面加的是异常对象;                    
   

J:RuntimeException:  

             1,RuntimeException异常,在函数上声明该异常,调用者不用处理,编译一样通过。函数内抛出该异常对象,函数不声明,编译也能通过。
             2,之所以不用再函数上声明,是因为不需要让调用者处理,当该异常发生时,希望程序停止,因为在程序运行时,出现了无法继续运算的情况,希望程序停止后,对代码进行修正。
             3,异常分类:编译时被检测异常;运行时异常,RuntimeException以及子类。
             4,自定义异常时:如果该异常的发生,无法在继续预算,就让该自定义异常继承RuntimeException。

     
【上篇】
【下篇】

抱歉!评论已关闭.