java中的interface接口實例詳解
java中的interface接口實例詳解
接口:Java接口是一些方法表征的集合,但是卻不會在接口里實現(xiàn)具體的方法。
java接口的特點如下:
1、java接口不能被實例化
2、java接口中聲明的成員自動被設置為public,所以不存在private成員
3、java接口中不能出現(xiàn)方法的具體實現(xiàn)。
4、實現(xiàn)某個接口就必須要實現(xiàn)里面定義的所有方法。
接下來看一個實現(xiàn)接口的案例:
package hello; interface competer{ //定義接口 void set_compt(int com); void print_compt_information(); } class bj implements competer{ //接口實現(xiàn) int com ; public void set_compt(int com) { this.com = com ; // System.out.println("端口" + com); } public void print_compt_information() { System.out.println("端口" + com); System.out.println("筆記本"); } } class taishi implements competer{ int com ; public void set_compt(int com) { this.com = com ; //System.out.println("端口" + com); } public void print_compt_information() { System.out.println("端口" + com); System.out.println("臺式機"); } } public class inter { public static void main(String[] args) { taishi com = new taishi(); bj bjj = new bj(); com.set_compt(100); bjj.set_compt(120); com.print_compt_information(); bjj.print_compt_information(); } }
運行結果:
端口100 臺式機 端口120 筆記本
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Java使用動態(tài)規(guī)劃算法思想解決背包問題
背包問題(Knapsack problem)是一種組合優(yōu)化的NP完全問題。問題可以描述為:給定一組物品,每種物品都有自己的重量和價格,在限定的總重量內,我們如何選擇,才能使得物品的總價格最高2022-04-04使用springboot單元測試對weblistener的加載測試
這篇文章主要介紹了使用springboot單元測試對weblistener的加載測試,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot自動配置@EnableAutoConfiguration過程示例
這篇文章主要為大家介紹了SpringBoot自動配置@EnableAutoConfiguration的過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10springboot整合websocket實現(xiàn)群聊思路代碼詳解
通過springboot引入websocket,實現(xiàn)群聊,通過在線websocket測試進行展示。本文重點給大家介紹springboot整合websocket實現(xiàn)群聊功能,代碼超級簡單,感興趣的朋友跟隨小編一起學習吧2021-05-05Java實現(xiàn)學生信息管理系統(tǒng)(使用數(shù)據(jù)庫)
這篇文章主要為大家詳細介紹了Java實現(xiàn)學生信息管理系統(tǒng),使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01