java中接口的简单运用&java中的一些异常(运用myeclipse)
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(); } } }