Java基础加强12-异常、泛型
前言:
这个系列记录了我学习面向语言Java的完整过程,可以当作笔记使用。
每篇文章都包含可运行的代码示例和常见错误分析,尤其适合没有编程经验的读者。学习时建议先准备好安装JDK(Java Development Kit)和IDEA(IntelliJ IDEA集成开发环境),随时尝试修改示例代码。
一,异常
1,认识异常:
Java的异常体系:
2,异常的作用:
作用1:
点击后快速定位出错的代码位置
作用2:
RuntimeExceptio,运行时异常,提醒不强烈
public static void main(String[] args) {// 目标:搞清楚异常的作用。System.out.println("程序开始执行...");System.out.println(div(10, 0));System.out.println("程序结束执行...");
}// 需求:求2个数的除的结果,并返回这个结果。
public static int div(int a, int b) {if (b == 0) {System.out.println("除数不能为0,您的参数有问题!");// 可以返回一个异常给上层调用者,返回的异常还能告知上层底层是执行成功了还是执行失败了!throw new RuntimeException("除数不能为0,您的参数有问题!");}int result = a / b;return result;
}
throw new RuntimeException("除数不能为0,您的参数有问题!");
充当了方法div的参数b的值为0时的返回值,然后抛出异常。
运行结果:
Exception,编译异常,提现强烈,需要进行处理
3,自定义异常:
缘由:
示例:
自定义编译时异常:
/*** 自定义的编译时异常* 1、继承Exception做爸爸。* 2、重写Exception的构造器。* 3、哪里需要用这个异常返回,哪里就throw*/
public class AgeIllegalException extends Exception {public AgeIllegalException(String message) {super(message);} // 有参构造器,调用了父类的有参构造器public AgeIllegalException() {super();} // 无参构造器,调了父类的无参构造器
}
自定义运行时异常:
public class AgellegalRuntimeException extends RuntimeException {public AgellegalRuntimeException(String message) {super(message);}public AgellegalRuntimeException() {super();}
}
show方法运用了自定义编译时异常,show2方法运用了自定义运行时异常.
public class Demo3Exception {public static void main(String[] args) {//目标:认识自定义异常try {show(100);} catch (AgellegalException e) {e.printStackTrace();//输出异常信息}System.out.println("程序继续执行");show2(1000);System.out.println("程序继续执行...");}//需求:我们公司的系统只要收到了年龄小于1岁或者大于200岁的就是一个年龄非法异常public static void show(int age) throws AgellegalException {if(age<1||age>200){//年龄非法,抛出一个异常返回throw new AgellegalException("年龄非法");}else{System.out.println("年龄合法");System.out.println("保存年龄:"+age);}}public static void show2(int age) {if (age < 1 || age > 200) {//年龄非法,抛出一个异常返回throw new AgellegalRuntimeException("年龄非法");} else {System.out.println("年龄合法");System.out.println("保存年龄:" + age);}}
}
注:
1,方法不需要返回值也可以抛出异常返回
2,示例的show2方法,虽然输入的参数不合法,但是如果要处理也可以用try{}catch{}处理。
3,只有对异常进行了处理从能继续执行代码,不然运行停止。
4,异常的处理方案:
方案2:
示例:
二,泛型
1,认识泛型:
2,泛型类:
package com.itheima.demo2genericity; // 泛型类 public class MyArrayList<E> {//省略…… }
3,泛型接口:
示例:
4,泛型方法:
5,通配符和泛型的上下限:
示例:
import java.util.ArrayList;public class GenericDemo5 {public static void main(String[] args) {//目标:理解通配符和上下限,只是理解即可//需求开发一个极品飞车的游戏//集合小米ArrayList<Xiaomi> xiaomis = new ArrayList<>();xiaomis.add(new Xiaomi());xiaomis.add(new Xiaomi());go(xiaomis);//集合比亚迪ArrayList<BYD> byds = new ArrayList<>();byds.add(new BYD());byds.add(new BYD());go(byds);//集合狗ArrayList<Dog> dogs = new ArrayList<>();dogs.add(new Dog());dogs.add(new Dog());// go(dogs);//public static void go(ArrayList<?> cars){} //所有类型的集合都可以,但是狗和车比不合理,要进行限制}// 注:虽然 Xiaomi 和 BYD 是 Car 的子类,但是 // ArrayList<Xiaomi> ArrayList<BYD>和 ArrayList<Car>是没有半毛钱关系!// go方法的形参类型如果是是:ArrayList<?>(集合),任意类型集合都可以// go方法的形参类型是ArrayList<?extends Car>(集合),只能是Car或者Car的子类,运用了泛型的上限//?是通配符public static void go(ArrayList<?extends Car> cars){}
}
6,泛型支持的类型:
可以通过包装类来间接实现基本数据类型:
包装类新增的功能:
示例:
//包装类新增的功能:
//1,把基本的类型数据转化为字符串
int j=23;
String s = Integer.toString(j);//"23"
System.out.println( s+1);//"231"Integer j2=j;//自动包装
String s2 = j2.toString();//"23"
System.out.println( s2+1);//"231"//2,把字符串转化为对应的基本数据类型(很有用)
String str = "98";
//int i3 = Integer.parseInt(str);
int i3 = Integer.valueOf( str);
System.out.println(i3+2);//100String str2 = "98.8";
//double d = Double.parseDouble(str2);
double d = Double.valueOf(str2);
System.out.println(d+2);//100.8