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

