java基础--9

来源:互联网 发布:淘宝贷款100万还不上 编辑:程序博客网 时间:2024/05/16 01:49

1、内部类访问规则
 内部类可以直接访问外部类中的成员,包括私有的
 外部类要访问内部类,必须建立内部类对象:Outer.Inner inner=new Outer().new Inner();
 内部类访问外部类中的成员变量:Outer.this.x
 当内部类被static修饰后,只能访问外部类中的static成员
 在外部其他类中,直接访问static内部类的非静态成员:new Outer.Inner().function();
 在外部其他类中,直接访问static内部类的静态成员:Outer.Inner.function();
 注意:
  当内部类中定义了静态成员,该内部类必须是静态的
  当外部类中的静态方法访问内部类时,内部类也必须是静态的
2、内部类定义原则
 事物内部还有事物
 class Body
 {
  private class Xinzang
  {
  }
  public void show()
  {
   new XinZhang();
  }
 }

 内部类定义在局部时,
  不可以被成员修饰符修饰
  可以直接访问外部类中的成员,因为还持有外部类中的引用。但是不可以访问它所在局部中的变量,只能访问被final修饰的局部变量
3、匿名内部类
 其实就是内部类的简写格式
 定义匿名内部类的前提:
  内部类必须继承一个类,或者实现接口
 匿名内部类的格式:new 父类或者接口(){定义子类的内容}
 其实匿名内部类就是一个匿名子类对象,而且这个对象有点胖
 匿名内部类中定义的方法最好不要超过3个
4、内部类练习
 public class InnerClassTest
{
 public static void main(String[] args)
 {
  Test.function().method();
 }
}

interface Inter
{
 public void method();
}

class Test
{
 static Inter function()
 {
  return new Inter()
  {
   public void method()
   {
    System.out.println("method run");
   }
  };
 }
}

异常
1、概述
 异常就是程序在运行时出现的不正常情况
 异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述。并封装成对象。
    其实就是Java对不正常情况进行描述后的对象体现
 对于问题的划分有两种:严重的,不严重的
  对于严重的java通过Error来描述。
   对于Error一般不编写针对性的代码进行处理
  对于不严重的java通过Exception来进行描述。
   对于Exception可以使用针对性的处理方式进行处理
 Throwable
  |--Error
  |--Exception
2、异常处理:try-catch-finally
 对捕获到的异常对象进行常见方法操作。
  String getMessage();获取异常信息(会自动调用toString()方法)
3、异常声明--throws
 在功能上通过throws的关键字声明了该功能有可能会出现问题
4、对多异常的处理
 声明异常时,建议声明更为具体的异常,这样处理的可以更具体。
 对方声明几个异常,就对应有几个catch块,不要定义多余的catch块。如果多个catch块中的异常出现继承关系,父类异常catch块,放在最下面
 在进行catch处理时,catch中一定要定义具体处理方式,不要简单定义一句e.printStackTrace(),也不要简单的就书写一条输出语句
  public class DuoException
{
 public static void main(String[] args)
 {
  Demo d=new Demo();
  try
  {
   //d.div(1,0);
   d.div(1,4);
  }
  catch(ArithmeticException e)
  {
   System.out.println("被零除了!!!");
  }
  catch(ArrayIndexOutOfBoundsException e)
  {
   System.out.println("数组越界了!!!");
  }
 }
}

class Demo
{
 int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
 {
  int[] arr=new int[b];
  System.out.println(arr[4]);
  return a/b;
 }
}
5、自定义异常
 class FuShuException extends Exception{}
 手动抛出异常 throw new FuShuException();
 当在函数内部出现了throw抛出异常,就必须要给对应的处理动作
  要么在内部try catch处理
  要么在函数上声明让调用者处理
 定义异常信息
  覆写构造方法和getMessage()方法
6、throws和throw的区别
 throws使用在函数上,throw使用在函数内
 throws后面跟的异常类,可以跟多个,用逗号隔开;throw后跟的是异常对象
 public class ExceptionDemo
{
 public static void main(String[] args)
 {
  Demo d=new Demo();
  try
  {
   int x=d.div(1,-9);
   System.out.println(x);
  }
  catch(FushuException e)
  {
   System.out.println(e.toString());
   System.out.println("被负数除了!!");
   System.out.println("这个负数是:"+e.getValue());
  }
 }
}

class FushuException extends Exception
{
 private int value;
 FushuException(String msg)
 {
  super(msg);
 }
 FushuException(String msg,int value)
 {
  super(msg);
  this.value=value;
 }
 public int getValue()
 {
  return value;
 }
}

class Demo
{
 int div(int a,int b)throws FushuException
 {
  if(b<0)
  {
   throw new FushuException("不能被负数除",b);
  }
  return a/b;
 }
}
7、RuntimeException运行时异常
 如果在函数内抛出该异常,函数上可以不用声明,编译一样通过。
 如果在函数上声明了该异常,调用者可以不用进行处理。编译一样通过
 之所以不用在函数上声明是因为不需要让调用者处理。当该异常发生,希望程序停止。
  因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码进行修正。
 自定义异常时,如果该异常的发生无法在继续进行运算时,就让自定义的异常继承RuntimeException
 对于异常分两种:
  编译时被检测的异常
  编译时不被检测的异常(RuntimeException及其子类)
8、练习
 public class ExceptionTest
{
 public static void main(String[] args)
 {
  Teacher t=new Teacher("好老师",new Computer());
  try
  {
   t.jiangke();
  }
  catch(NoPlanException e)
  {
   System.out.println("放假");
  }
 }
}

class Teacher
{
 private String name;
 private Computer c;
 Teacher(String name,Computer c)
 {
  this.name=name;
  this.c=c;
 }
 public void jiangke()throws NoPlanException
 {
  try
  {
   c.run();
  }
  catch(LanPingException e)
  {
   c.reset();
  }
  catch(MaoYanException e)
  {
   throw new NoPlanException("课程计划无法进行"+e.toString());
  }
  System.out.println("讲课");
 }
}

class Computer
{
 private int state=3;
 public void run()throws LanPingException,MaoYanException
 {
  if(2==state)
  {
   throw new LanPingException("蓝屏了!!!");
  }
  if(3==state)
  {
   System.out.println("test");
   throw new MaoYanException("冒烟了!!!");
  }
  System.out.println("电脑运行");
 }
 public void reset()
 {
  state=1;
  System.out.println("电脑重启");
 }
}

class LanPingException extends Exception
{
 LanPingException(String msg)
 {
  super(msg);
 }
}

class MaoYanException extends Exception
{
 MaoYanException(String msg)
 {
  super(msg);
 }
}

class NoPlanException extends Exception
{
 NoPlanException(String msg)
 {
  super(msg);
 }
}

 


 

原创粉丝点击