package test;//创建一个名为test的包

public class A4paper implements Paper {
public String getSize(){
return"A4";//实现接口Paper
}
}
package test;

public class B5paper implements Paper {
public String getSize(){
return"B5";//实现接口Paper
}
}
package test;

public class ColorMohe implements Mohe {
public String getColor(){
return "colorful";//实现接口Mohe
}
}
package test;

public interface Mohe {
public String getColor();//创建接口Mohe
}
package test;

public interface Paper {
public String getSize();//创建接口Paper
}
package test;

public class Printer {
private Mohe mohe;
private Paper paper;
public void print(){
System.out.println("用"+mohe.getColor()+"打印"+paper.getSize()+"纸");
}
public Mohe getMohe(){
return mohe;
}
public void setMohe(Mohe mohe){
this.mohe=mohe;
}
public Paper getPaper(){
return paper;
}
public void setPaper(Paper paper){ this.paper=paper;//完成Mohe与Paper的封装
}
}
package test;

public class Test {
public static void main(String[] args){
Printer printer=new Printer();
Mohe mohe=new WhiteMohe();
printer.setMohe(mohe);
Paper paper=new A4paper();
printer.setPaper(paper);
printer.print();//实现用白墨盒打印A4纸的功能
}
}
package test;

public class WhiteMohe implements Mohe{
public String getColor(){
return "white";//实现接口Mohe
}
}

如上代码可以实现不同颜色墨盒打印打印不同类型纸张的功能。

下面谈谈java中的异常,异常分Error与Exception,主要讲讲Exception,java中Exception异常很多,常见的有下面5种

InputMismatchException 输入不匹配异常
ArithmeticException 算术异常
ArrayIndexOutOfBoundsException 数组下标越界异常
NullPointerException 空指针异常
NumberFormatException 数字格式转换异常

1.如何捕获异常:使用try-catch、finally语句

package test1;
import java.util.*;
import java.util.Scanner; public class Demo00 {
public static void main(String[] args){
try {
Scanner scanner=new Scanner(System.in);
System.out.println("");
int num1=scanner.nextInt();
int num2=scanner.nextInt();
System.out.println(num1+"/"+num2+"="+num1/num2);
System.out.println("thank you for your use");
}
catch (InputMismatchException e) {
System.err.println("输入不匹配异常");
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ArithmeticException e) {
System.err.println("算术异常");
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
System.out.println("其他异常");
// TODO Auto-generated catch block
e.printStackTrace();
return;//在finally语句后执行
}
finally {
System.out.println("thank you");
}
}
}

如上代码实现了输入对应的除数与被除数完成除法的功能,在用户输入了对应类型的错误输入值后,控制台上就会以红色文字显示出了哪种错误。代码执行顺序是try语句-->catch语句(非return)-->finally-->catch语句中return

2.throws声明异常

案例:
public class Demo05 {
//1.创建一个方法,声明异常
public void show()throws Exception{
//xxx
System.out.println("*******");
} //2.调用该方法的第一种方式,就是在调用方throws Exception
// public static void main(String[] args)throws Exception {
// Demo05 demo05 = new Demo05();
// demo05.show();
// } //3.用该方法的第二种方式,就是使用try catch捕获异常
public static void main(String[] args) {
Demo05 demo05 = new Demo05();
try {
demo05.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}

3.手工抛异常throw new Exception("");

案例:
public class Demo06 {
public void show() throws Exception {
int a = 3;
int b = 1;
if (a > b) {
System.out.println("a>b");
} else if (a <= b) {
System.out.println("a<=b");
} else {
// 手工抛异常
throw new Exception("程序有问题!");
}
} // public static void main(String[] args)throws Exception {
// Demo06 demo06 = new Demo06();
// demo06.show();
// } public static void main(String[] args) {
Demo06 demo06 = new Demo06();
try {
demo06.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}

最新文章

  1. [No000075]有没有安全的工作?
  2. iOS并发编程指南之同步
  3. centos时间同步方法
  4. 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
  5. Java JDBC连接数据库 Access连接数据库
  6. sealed(C# 参考)
  7. mysql 调用带返回值的存储过程
  8. Android 内存管理(二)
  9. shell脚本编程常识
  10. Collection&lt;E&gt;、Iterable&lt;T&gt;和Iterator&lt;E&gt;接口
  11. 如何更改Linux的ssh端口
  12. rpm包
  13. Python内置函数(9)——callable
  14. css3 实现动画
  15. 【iCore4 双核心板_uC/OS-II】例程十:信号量集
  16. HDU.4352.XHXJ&#39;s LIS(数位DP 状压 LIS)
  17. HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)
  18. VCL 中的 Windows API 函数(2): ActivateKeyboardLayout
  19. Bypass X-WAF SQL注入防御(多姿势)
  20. chapter02 朴素贝叶斯分类器对新闻文本数据进行类型预测

热门文章

  1. USACO 5.5 章节
  2. 【刷题笔记】686. Repeated String Match
  3. 怎么追加byte内容
  4. java反射(四)--反射与简单java类
  5. object of type &#39;Response&#39; has no len()
  6. SET SESSION AUTHORIZATION - 为当前会话设置会话用户标识符和当前用户标识符
  7. Linux Centos 7 下部署 .NetCore + MySql + Redis + mssql2007 部署过程
  8. 力扣—climbing stairs(爬楼梯) python实现
  9. vue+cesiumjs环境搭建【import引入】
  10. Linq 使用Startswith 出现空引用的问题