案例1:自定义年龄异常
需求:
创建一个Person类,提供get/set方法,
在setAge方法中判断年龄是否符合(0-130)这个范围,
如果不符合抛出一个对象,提示:年龄不符合!
a.我们需要自定义异常,来描述年龄不在0-130范围这种情况
建立一个自定义异常类:AgeOutOfRangeException
public class AgeOutOfRangeException extends Exception {
public AgeOutOfRangeException(){}
public AgeOutOfRangeException(String message){
super(message);
}
}
建立一个Person类
public class Person {
private String name;
private int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", 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 AgeOutOfRangeException {
if(age >= 0 && age <= 130){
this.age = age;
}else{
throw new AgeOutOfRangeException("您的年龄" + age + "不符合(0-130)!");
}
}
}
建立测试类:Test
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.setName("张三");
try{
p.setAge(140);
}catch (Exception e){
e.printStackTrace();
}
System.out.println(p);
}
}
案例2:多线程同时做多件事情
需求:
使用多线成实现同时做多件事情
public class Test {
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
System.out.println("打游戏!");
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("听歌");
}
}).start();
System.out.println("吃瓜子");
}
}
习题1
需求:
定义一个方法,功能是:可以获取一个int[]数组中的最大值。请实现此方法,并针对可能出现的异常情况使用throw抛出相应的异常,并给出提示信息。
public class Demo {
public static void main(String[] args) {
int[] arr = {};
if(arr.length <= 0){
throw new ArrayIndexOutOfBoundsException("数组中没有元素");
}
getMax(arr);
}
public static void getMax(int[] arr) {
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if(max < arr[i]){
max = arr[i];
}
}
System.out.println(max);
}
}
习题2
需求:
请使用代码实现
- 每一个学生(Student)都有学号,姓名和分数,分数永远不能为为负数。
- 如果试图给学生赋值一个负数,抛出一个自定异常 。
建立一个异常类:PriceOutOfRangeExcepion
public class PriceOutOfRangeExcepion extends Exception{
public PriceOutOfRangeExcepion(String message) {
super(message);
}
public PriceOutOfRangeExcepion() {
}
}
建立一个学生类:Student
public class Student {
private String id;
private String name;
private int price;
@Override
public String toString() {
return "Stuednt{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", price=" + price +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) throws PriceOutOfRangeExcepion {
if(price >= 0){
this.price = price;
}else{
throw new PriceOutOfRangeExcepion("你的成绩" + price + "不能小于0");
}
}
}
建立一个测试类:Test
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.setId("001");
stu.setName("张三");
try {
stu.setPrice(100);
} catch (PriceOutOfRangeExcepion priceOutOfRangeExcepion) {
priceOutOfRangeExcepion.printStackTrace();
}
System.out.println(stu);
}
}
习题3
需求:
编写程序,创建两个线程对象,一根线程输出“播放背景音乐”,另一根线程输出“显示画面”,要求线程实现 Runnable接口。
public class Demo {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("播放背景音乐");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("显示画面");
}
}).start();
}
}