Lambda表达式、异常
一、匿名内部类
匿名对象:没有引用指向的对象,只能在创建的时候使用一次
匿名内部类:没有名字的内部类
简化没有类本身作用,只为了重写的一些抽象方法,方便调用这个方法 ,匿名内部类就可以简化这种实现类
Swim s=new Swim(){
@Override
System.out.println("游泳");
}
s.swim();
二、Lambda
jdk8新增功能
作用:为了简化匿名内部类结构
前提:函数式接口:只存在一个必须被重写的抽象方法的接口
判断方式:@FunctionalInterface
//1.lambda写法1
s = () -> {
System.out.println("游泳...");
};
//2.lambda写法2 : 当lambda体中的语句体只有一句,前后的{}可以省略
s = () ->System.out.println("游泳...");
//3.lambda写法3 : 参数列表的数据类型可以省略
s = (i) ->System.out.println("游泳...");
//4.Lambda写法4 : 如果参数只有一个,前后的()可以省略 ,如果参数个数为2~多个,前后()不能省略
s = i ->System.out.println("游泳..."+i);
//5.Lambda写法5 : 如果方法存在返回值,并且{}中语句体只有一句,为return 带出返回值语句,前后的{}与return关键字可以一起省略
s = (a,b) ->{return a+b;};
s = (a,b) ->a+b;
三、异常
1、Throwable
程序出现了问题
程序一旦遇到异常,后面代码无法正常执行,并且同时控制台中展示异常的详细信息|细节问题,便于程序员的调试
异常体系:
Error 错误:一般指由虚拟机生成并脱出的,无需程序员解决处理
Exception 异常:异常如果不处理,程序无法正常执行,需要程序员在代码层面上处理异常
CheckedException 编译时异常|检查时异常 : 编译期间发生的异常,不处理程序无法运行
RuntimeException 运行时异常 : 运行期间才会发生的异常
重点学习关注异常处理方案:
所有的异常都可以通过标准异常处理方案来处理
运行时异常一般通过增强程序健壮性的代码就可以解决 -> if判断
编译时异常只能通过异常处理方案来处理->try{}catch(){}finally{}
常见的异常:
NullPointerException 空指针异常
ArithmeticException 数学异常
ArrayIndexOutOfBoundsException 数组索引越界异常
NegativeArraySizeException 数组长度负数异常
ClassCastException 类型转换异常
NumberFormatException 转换格式异常
2、异常的解决方式
1.异常抛出 throws
把异常抛出到上一层,谁调用谁处理
2.异常捕获
异常对象当前抓住捕获,直接处理
public class ExceptionTest {
public static void main(String[] args) {
try{
//ArithmeticException
test();
//NegativeArraySizeException
int[] arr1=new int[-1];
//ArrayIndexOutOfBoundsException
int[] arr2=new int[3];
System.out.println(arr2[3]);
//NullPointerException
String str=null;
str.toString();
}catch(Exception e){
e.printStackTrace();
System.out.println("出现异常!");
}finally {
System.out.println("结束方法");
}
}
public static void test() throws ArithmeticException{
System.out.println(1/0);
}
}
3、自定义异常
自定义异常
异常类也是类
需要直接或者间接的继承自Exception
如果是运行时期异常必须直接或者间接的继承RuntimeException
制造异常: throw
//编译时异常的定义
public class ExceptionThrow {
public static void main(String[] args) {
Person p=new Person();
p.setName("张三");
try {
p.setAge(20);
} catch (AgeException e) {
e.printStackTrace();
}finally {
System.out.println("结束");
}
System.out.println(p);
}
}
class AgeException extends Exception{
public AgeException(){super();}
public AgeException(String s){super(s);}
}
class Person{
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException{
if(age<18){
throw new AgeException("年龄"+age+"不合法");
}
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
//运行时异常的定义
public class ExceptionThrow {
public static void main(String[] args) {
Person p=new Person();
p.setName("张三");
int age=20;
if(age>=18){
p.setAge(age);
}
System.out.println(p);
}
}
class AgeException extends RuntimeException{
public AgeException(){super();}
public AgeException(String s){super(s);}
}
class Person{
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age){
if(age<18){
throw new AgeException("年龄"+age+"不合法");
}
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}