接口和异常
接口
接口的關(guān)鍵字是interface,接口名首字母大寫,寫法是interface 類名(){};
?
?創(chuàng)建一個接口:
1 public interface NewFeat { 2 String a="NewFeat"; 3 //視頻播放 4 public void video(); 5 6 //音頻播放 7 public void audio(); 8 }
?接口的實現(xiàn):
1 //實現(xiàn)NewFeat接口 2 public class NewFeatImp implements NewFeat{ 3 4 @Override 5 public void video() { 6 System.out.print("我能播放視頻!"); 7 } 8 @Override 9 public void audio() { 10 System.out.print("我能播放音頻!"); 11 } 12 13 }
?
?
?什么是接口:
①概念性的接口,系統(tǒng)對外提供的所有服務(wù)
②實在的接口,使用interface關(guān)鍵字所修飾的類
接口的特性和使用:
①接口不可以被實例化,也沒有構(gòu)造方法
②實現(xiàn)類必須實現(xiàn)接口的所有方法
③實現(xiàn)類可以實現(xiàn)多個接口
用implements關(guān)鍵字,多個接口使用逗號隔開
④接口中的變量都是靜態(tài)常量(public static final)
⑤接口中的方法默認都是公共抽象方法(public abstract)
⑥接口不能實現(xiàn)其他接口,但是可以繼承多個其他接口,用extends關(guān)鍵字,多個接口用逗號隔開
?
接口和抽象類的區(qū)別:
相同點:
都代表系統(tǒng)的抽象層
都不能被實例化 都能包含抽象方法 用于描述系統(tǒng)提供的服務(wù),不必提供具體實現(xiàn)
不同點:
在抽象類中可以為部分方法提供默認實現(xiàn),而接口中只能包含抽象方法
抽象類便于復(fù)用,接口便于代碼維護
一個類只能繼承一個直接的父類,但可以實現(xiàn)多個接口
?
異常
?
異常是指在程序的運行過程中所發(fā)生的不正常的事件,它會中斷正在運行的程序。
Java的異常處理是通過5個關(guān)鍵字來實現(xiàn)的:try、catch、 finally、throw、throws
?
try-catch-finally結(jié)構(gòu):
?不論是否有異常,finally塊中的語句都會執(zhí)行。
1 public class TestException { 2 public static void main(String[] args){ 3 Scanner in=new Scanner(System.in); 4 System.out.print("請輸入第一個數(shù):"); 5 try{ 6 int n1=in.nextInt(); 7 System.out.print("請輸入第二個數(shù):"); 8 int n2=in.nextInt(); 9 System.out.println("兩數(shù)之商為:"+(n1/n2)); 10 }catch(InputMismatchException e){ 11 System.out.println("獲取到了異常!"); 12 e.printStackTrace(); 13 //退出虛擬機 14 //System.exit(1); 15 }finally{ 16 System.out.println("感謝使用!"); 17 } 18 } 19 }
?
運行結(jié)果:
?
注: try-catch-finally結(jié)構(gòu)中try語句塊是必須的,catch、finally語句塊均可選,但兩者至少出現(xiàn)之一
當finnally前面有System.exit(1)時,finally塊中的語句不執(zhí)行。
?
?
throw和throws:
實例:
1 public class Persion { 2 private int age; 3 public int getAge() { 4 return age; 5 } 6 public void setAge(int age) throws Exception { 7 if(1<=age&&age<=100){ 8 this.age = age; 9 }else{ 10 //異常拋出 11 throw new Exception("年齡輸入錯誤!"); 12 } 13 14 } 15 16 }
?創(chuàng)建測試類,
1 public class TestPersion { 2 public static void main(String[] args){ 3 Persion p=new Persion(); 4 //獲取異常并進行處理 5 try{ 6 p.setAge(209); 7 System.out.print("年齡是:"+p.getAge()); 8 }catch(Exception e){ 9 //輸出異常 10 e.printStackTrace(); 11 } 12 13 } 14 15 }
輸出結(jié)果:
?
異常鏈:
異常鏈創(chuàng)建了新的異常但卻保留了原有異常的信息
創(chuàng)建一個用戶類:
1 public class User { 2 private String name;//用戶名 3 private String password;//用戶密碼 4 public String getName() { 5 return name; 6 } 7 public void setName(String name) { 8 this.name = name; 9 } 10 public String getPassword() { 11 return password; 12 } 13 public void setPassword(String password) { 14 this.password = password; 15 }
創(chuàng)建一個登陸類:
1 public class Login { 2 private String lname;//登錄名 3 private String lpassword;//登錄密碼 4 5 public String getLname() { 6 return lname; 7 } 8 public void setLname(String lname) { 9 this.lname = lname; 10 } 11 public String getLpassword() { 12 return lpassword; 13 } 14 public void setLpassword(String lpassword) { 15 this.lpassword = lpassword; 16 } 17 18 }
自定義兩個異常:
1 public class RegFalseException extends Exception{ 2 3 public RegFalseException(){} 4 5 public RegFalseException(String message, Throwable cause) { 6 super(message, cause); 7 } 8 public RegFalseException(String masg){ 9 super(masg); 10 } 11 12 }
1 public class LogFalseException extends Exception{ 2 3 public LogFalseException() { 4 super(); 5 } 6 public LogFalseException(String message, Throwable cause) { 7 super(message, cause); 8 } 9 10 public LogFalseException(String message) { 11 super(message); 12 } 13 14 }
創(chuàng)建測試類:
1 public class TestCase { 2 public static void main(String[] args){ 3 User use=new User(); 4 try { 5 Log(use); 6 } catch (LogFalseException e) { 7 e.printStackTrace(); 8 } 9 } 10 //用戶注冊方法 11 public static void Reg(User use) throws RegFalseException{ 12 if(use.getName()==null){ 13 throw new RegFalseException("注冊失敗!"); 14 }else{ 15 System.out.print("注冊成功!"); 16 } 17 } 18 //用戶登錄方法 19 public static void Log(User use) throws LogFalseException{ 20 try{ 21 Reg(use); 22 System.out.print("登錄成功!"); 23 }catch(RegFalseException e){ 24 e.printStackTrace(); 25 throw new LogFalseException("登錄失敗",e); 26 } 27 } 28 29 }
運行結(jié)果:兩種異常都輸出了
?
轉(zhuǎn)載于:https://www.cnblogs.com/TFE-HardView/p/10955602.html
總結(jié)
- 上一篇: 约束文件配置
- 下一篇: 第三周-第08章节-Python3.5-