传智播客——毕向东25--第9天总结-8

1.内部类访问规则:
 将一个类定义在另一个类的里面,对里面的那个类就称为内部类(内置类、嵌套类)
 访问特点:
 1.内部类可以直接访问外部类中的成员包括私有成员:内部类之所以能直接访问外部类成员,是因为内部类中
   持有了一个外部类的应用:格式:外部类名.this;


 2.而外部类要访问内部类中的成员必须要简历内部类对象
 class Outer
 {
  private int x=3;
  class Inner //内部类:可以被私有修饰 private class Inner
  {
   void function()
   {
    System.out.println(Outer.this.x);
   }
  }
  void method
  {
   Inner in=new Inner();
   in.function();
  }
 }
 class Demo
 {
  public static void main(String[] args)
  {
   Outer out=new Outer()
    out.method();
   //直接访问内部类中的成员
   //Outer.Inner in= nre Outer().new Inner();
   //in.function();
  }
 }
2.静态内部类
a.当内部类定义在外部类的成员位置上,而且非私有可以在外部其他类中可以直接建立内部类对象。
 格式:
  外部类名.内部类名  变量名=外部类对象.内部类对象
b.内部类在成员位置上时,可以被成员修饰符修饰
         例如: private:将内部类在外部类中进行封装
          static:内部类就具备static特性
c.当内部类被static修饰后,只能直接访问外部类中的static成员
d.访问static内部类中的非静态成员:new Outer.Inner().fun();
  ......................静态成员:Outer.Inner.fun();
注意:当类中定义了静态成员,该内部类就必须是static的。
      当外部类中国的静态方法访问内部类时,内部类也必须是static的
3.内部类定义原则:
当描述事物时,事物的内部还有事物,该事物用内部类来描述
4.匿名内部类:
a.匿名内部类其实就是内部类的简写格式
b.定义匿名内部类的前提: 内部类必须是继承一个类或者实现接口
c.匿名内部类的格式:new 父类或接口(){定义子类内容}
d.匿名内部类的方法最好不要超过3个
interface class AbsDemo
{
 abstract void show();
}
class Outer
{
 int x=3;
 /*
 class Inner extends AbsDemo
 {
  void show()
     {
   System.out.println("show:"+x);
     }
  void abc()
     {Sytem.out.println("abc");}
 }
 */
 public void function()
 {
  //new Inner().show();
  AbsDemo d=new AbsDemo()
  {
   void show()
   {
    System.out.println("x=="+x);
   }
   void abc()
   {
    System.out.println("haha");
   }
  }
  d.show();
  //d.abc();//编译失败,父类无此方法
  class Inner classDemo4
  {
   public static void main(String[] args)
   {
    new Outer().function();
   }
  }
 }
 }
练习:补全代码
interface Inter
{
 void method();
}
class Test
{
 //补全代码,通过匿名类
 /*static class Inner implement Inter
 [
  public void method()
     {
   System.out.println("method run");
  }
 }
 */
 static Inter function()
 {
  return new Inter
  {
   public void Inter()
   {
    System.out.println("method run");
   }
  };
 }
}
class InnerClassTest
{
 public static void main(String[] args)
 {
  Test.function().method();
  //Test.function():Test类中有一个静态方法function
  //.method():function这个方法运算后的结果是一个对象,而且是Inter类型的对象,因为只有是Inter类型的对象才可以调用method方法
 }
}
4.异常:
   Throwable
      |---Error
   |---Exception
   异常的处理:
   try
   {
  需要检测的代码;
   }
   catch (异常类 变量)
   {
    处理异常的代码(处理方式);
   }
   finally
   {
    一定执行的语句;
   }
 e.getmessage();//异常信息
 e.toString();//异常名称,信息
 e.printStackTrace();//异常名称,信息,位置(JVM默认)
 异常声明throws
 方法() throws Exception //声明该功能可能会出现问题

 多异常处理:
 ArithmeticException:算数异常
 ArrayIndexOutOfBoundsException:下标越界异常
 
 class Demo
 {
  int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException
  {
   int[] arr=new int[a];
   System.out.println(arr[4]);
   return a/b;
  }
 }
 class ExceptionDemo
 {
  public static void main(String[] args)
  {
   Demo d=new Demo();
   try
   {
   int x=d.div(5,0)
   System.out.println("x="+x);
   }
   catch (ArithmeticException e)
   {
    System.out.println(e.toString());
   }
    catch (ArrayIndexOutOfBoundsException e)
   {
    System.out.println(e.toString());
   }
  }
 }
自定义异常:
 class FuShuException extends Exception
 {
  private int value;
  FuShuException
  {
   super();
  }
  FuShuException(String message,int value)
  {
   super(message);
   this.value=value;
  }
  public int getValue()
  {
   return value;
  }
 }

当在函数内部出现了throw抛出异常对象,就必须要给对对应的处理动作
     a.要么内部try处理
  b.要么在函数上声明让调用者处理
函数内出现异常需要声明
   class Demo
   {
    int div(int a,int b)throws FuShuException
    {
     if(b<0)
      throw new FuShuException("出现了异常",b) //throw关键字跑出已个自定义异常
    }
   }
throw和throws的区别:
   throws使用在函数上,throws后面跟的异常类,可以跟对个,用逗号隔开
   throw使用在函数内, throw后面跟的是异常对象
 
 RuntimeException:
 运行时异常
   RuntimeException异常可以不用声明
   当一场发生时,希望异常程序停止,因为在运行时,出现了无法继续运算的情况
   自定义异常时,可以让异常类继承RuntimeException
    异常分为两种:
    a.编译时被检测的异常
    b.编译时不被检测的异常(运行时异常,RuntimeException以及其子类)
练习:
..............
  public void prelect()throws NoPlanException   //声明异常
  {
   try
   {
  cmtp.run();
   }
   catch (LanPingException e)
   {
    cmpt.rest();
   }
   catch (MaoYanException e)
   {
    throw new NoplanException("课时无法继续"); //抛出新异常,需要在函数上声明该异常
   }
  }
.......

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值