Day 9
内部类:
在描述事物时,事物的内部还有事物,这个事物可以用内部类来描述。
内部类的访问规则:
内部类可以直接访问外部类的成员,而外部类访问内部类成员时,要建立内部类对象。
格式是 外部类.内部类=new外部类().new内部类()
直接建立一个内部类的对象时必须标明内部类所属的外部类。
建立外部类对象来调用内部类。
class demo
{
int x=1;
void show1()
{
indemo s=new indemo();
s.show();
}
class indemo
{
void show()
{
System.out.println("x="+x);
}
}
}
class day09
{
public static void main(String[] args)
{
demo x=new demo();
x.show1();
}
}
直接建立内部类对象:
class demo
{
int x=1;
class indemo
{
void show()
{
System.out.println("x="+x);
}
}
}
class day09
{
public static void main(String[] args)
{
demo.indemo x=new demo().new indemo();
x.show();
}
}
内部类可以被私有修饰:
当内部类定义在外部类的成员位置上时,可以直接建立内部类对象,在成员位置上的内部类,可以被成员修饰符所修饰。
静态static修饰的内部类:
静态内部类:当内部类被静态修饰后,只能直接访问外部类中的静态成员。
外部其他类访问静态内部类中的非静态成员 直接建立对象访问。new 外部类 .内部类.成员。
当内部类中定义了静态成员,这个内部类必须是static的。
当外部类是静态的,访问的内部类成员也是静态的。
内部类定义在局部变量位置时:
不可以被成员修饰符修饰
可以直接访问外部类中的成员,因为他还被外部类引用,但是不可以访问局部变量,除非局部变量被final修饰。
匿名内部类:
将定义内部类简写。
abstract class dad
{
abstract void shows();
}
class demo
{
int x=1;
public void show()
{
new dad()
{
void shows()
{
System.out.println("nonameshow");
}
}.shows();
}
}
class day09
{
public static void main(String[] args)
{
demo s=new demo();
s.show();
}
}
异常
就是程序在运行时出现的不正常情况。
处理异常
try catch:处理异常程序的语句,try中定义的是要检测的语句,catch中定义解决问题的方法。
class demo
{
int result(int x,int y)
{
return x / y;
}
}
class day09
{
public static void main(String[] args)
{
int num;
demo x = new demo();
try
{
num = x.result(6, 3);
System.out.println("num=" + num);
}
catch(Exception S)
{
System.out.println("You can't /0 : )");
}
}
}
catch中含有一个参数(Exception x),在try语句检测出异常后,建立一个对象 Exception x=new ArithmeticException();这个对象x就被抛给了catch来执行。
**throws **方法标识符,对可能存在问题的方法进行标识,让调用者来解决这个问题,如果不解决,程序就会编译失败。
当try检测到异常,抛给java虚拟机,java虚拟机会调用方法来打印栈中的错误信息。
多异常:
当一个方法中出现多个异常时,进行多异常操作,多异常时标记异常要尽量详细,针对不同的异常要给出不同的处理方法来针对处理。
class demo
{
int result(int x,int y)throws ArithmeticException,ArrayIndexOutOfBoundsException
{
int [] arr=new int[x];
System.out.println("arr="+arr[y]);
return x / y;
}
}
class day09
{
public static void main(String[] args)
{
int num;
demo x = new demo();
try
{
num = x.result(6, 1);
System.out.println("num=" + num);
}
catch(ArithmeticException S)
{
System.out.println("You can't /0 : )");
}
catch(ArrayIndexOutOfBoundsException S)
{
System.out.println("arr out of limit");
}
}
}
如果catch发生了继承关系,父类catch代码块应该放在最下面。
自定义异常
throw
抛出异常对象,用于抛出异常对象处理,抛出的对象必须与Thorwable,Exception或者Error有关系
当我们需要定义不同的条件时来作为异常时,需要自定义异常,自定义异常需要自定义一个错误类继承Expection,构造函数来接收一个参数,父类来处理这个参数,在可能出现异常的方法后面要用throws关键字将异常标出。异常条件下,如果发生异常,创建一个异常类型对象并使用throw关键字抛出。在主函数中,try-catch使用方法一致。
class wrong extends Exception
{
wrong(String mes)
{
super(mes);
}
}
class mark
{
int num (int a,int b)throws wrong
{
if(b==1)
throw new wrong("除数不能是1哦!");
else
return a/b;
}
}
class demo
{
public static void main(String[] args)
{
mark res=new mark();
try
{
int sult = res.num(4, 4);
System.out.println("result=" + res);
}
catch (wrong e)
{
System.out.println(e.getMessage());
}
}
}
throw和throws的区别:
throws用在方法上,throw用在方法内。
throws后跟的是异常类,throw后跟的是异常对象。
RuntimeException
RuntimeException的子类不需要声明就可以在函数内抛出,程序编译不会报错 。
当程序有异常抛出时,catch内的语句结束,throw下的语句不会执行。
异常finally
位于catch下,finally中的语句一定会被执行。无论出现还是没有出现异常,finally中的语句一定会被执行。
class demo
{
int ARR(int x)throws ArrayIndexOutOfBoundsException
{
int [] arr = new int[4];
if(x<0)
throw new ArrayIndexOutOfBoundsException();
return arr[x];
}
}
class day09
{
public static void main(String[ ] args)
{
demo a=new demo();
try
{
int num;
num=a.ARR(-1);
System.out.println("arr="+num);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("角标错误");
}
}
}
子类抛出异常,只能抛出父类的异常或者异常的子类。