1 //异常类 2 package st; 3 public class example_1 4 { 5 public static void main(String args[]) 6 { 7 int n=0,m=0,t=1000; 8 try 9 { 10 m=Integer.parseInt("8888"); 11 n=Integer.parseInt("ab89"); 12 t=7777; //t没有机会被赋值 13 } 14 catch(NumberFormatException e) 15 { 16 System.out.println("发生异常:"+ e.getMessage()); 17 } 18 System.out.println("n="+n+",m="+m+",t="+t); 19 try 20 { 21 System.out.println("故意抛出I/O异常!"); 22 throw new java.io.IOException("我是故意的"); //故意抛出异常 23 24 } 25 catch(java.io.IOException e) 26 { 27 System.out.println("发生异常:"+e.getMessage()); 28 } 29 } 30 }
/* 发生异常:For input string: "ab89" n=0,m=8888,t=1000 故意抛出I/O异常! 发生异常:我是故意的 */
1 package st ; 2 /* 和接口有关的匿名类 */ 3 interface SpeakHello 4 { 5 void speak(); 6 } 7 class HelloMachine 8 { 9 public void turnon(SpeakHello hello) 10 { 11 hello.speak(); 12 } 13 } 14 public class example_1 15 { 16 public static void main(String args[]) 17 { 18 HelloMachine machine =new HelloMachine(); 19 machine.turnon( new SpeakHello(){ 20 public void speak(){ 21 System.out.println("hello ,you are welcome !"); 22 } 23 } 24 ); 25 machine.turnon( new SpeakHello() 26 { 27 public void speak() 28 { 29 System.out.println("你好,欢迎光临!"); 30 } 31 } 32 ); 33 } 34 }
/* hello ,you are welcome ! 你好,欢迎光临! */