java 使用接口便于維護程序_Java初學——面向對象接口的應用

一、接口

1.什么是接口

接口是比抽象類更抽象的定義,接口不可以被實例化 實現類必須實現接口的所有方法 實現類可以實現多個接口 、多個接口使用逗號隔開 接口中的變量都是靜態常量(public static final)?程序設計時面向接口的約定而不考慮具體實現 。

2.為什么使用接口

有些事物具有相同的功能,多個類可以做相同的功能,程序設計中,要做到將功能模塊化,細分化,這樣有利于代碼的改寫,減少代碼冗余度,接口和繼承類似,但是繼承具有單根性,所以有了接口這個定義。

3.怎么使用接口

接口中的成員變量 默認都是public static final的,必須顯式初始化 接口中的方法 默認都是public abstract的 ,接口沒有構造方法,不能被實例化 一個接口不能實現另一個接口,但可以繼承多個其他接口 一個類必須實現接口抽象方法,除非這個類也是抽象類

4.接口與抽象類的區別

相同點 代表系統的抽象層 都不能被實例化 都能包含抽象方法 用于描述系統提供的服務,不必提供具體實現

不同點 在抽象類中可以為部分方法提供默認實現,而接口中只能包含抽象方法 抽象類便于復用,接口便于代碼維護 一個類只能繼承一個直接的父類,但可以實現多個接口

二、利用接口完成問題

問題:墨盒和紙張的規格是一種約定 打印機需要遵守這些約定 用面向接口編程的方式開發 制定墨盒、紙張的約定或標準 其他廠商按照墨盒、紙張的標準生產墨盒、紙張 打印機廠商使用墨盒、紙張的標準開發打印機

分析:墨盒和紙張規格是個接口,需要創建類去分別實現紙張和墨盒的接口,創建打印機類去組裝墨盒和紙張打印,最后創建測試類

1.創建紙張接口

//紙張的接口

public interfacePaper {

String newline="\n";//紙張都會有換行符所以定義在接口里//寫入字符的功能

void putChar(charword);//讀取紙張上內容的功能

String getContent();

}

2.創建墨盒的接口

//創建墨盒接口

public interfaceInk {//返回指定顏色

String getColor(int r,int g,intb);

}

3.實現墨盒接口

public class ColorInk implementsInk{

@Overridepublic String getColor(int r, int g, intb) {

Color color=new Color(r,g,b);//創建color對象

return "#"+Integer.toHexString(color.getRGB()).substring(2);

}

}

4.實現紙張接口

//紙張實現類

public class TextPaper implementsPaper{int linewords=16;//定義一行有16個字符

int rows=5;//一樣有五行

int x=0;int y=0;int paper=1;

String content="";

@Overridepublic void putChar(charword) {

content+=word;

x++;//移動字符的位置

if(x==linewords){

content+=newline;

x=0;

y++;

}if(y==rows){

content+="=======第"+paper+"頁=======";

paper++;

y=0;

content+=newline+newline;

}

}

@OverridepublicString getContent() {//獲取內容的階段

if(!(x==0&&y==0)){//頁中是否存在空行 lines-y=空行 \n

int count=rows-y;for(int i=0;i

content+=newline;

}

content+="=======第"+paper+"頁=======";

}returncontent;

}

}

5.組裝墨盒

//打印機類組裝墨盒和紙張

public classPrinter {private Ink ink; //墨盒

private Paper paper; //紙張

public voidprint(String content){

System.out.println("該打印機使用的顏色是:"+ink.getColor(50, 50, 50));for (int i = 0; i < content.length(); i++) {char c=content.charAt(i);

paper.putChar(c);

}

System.out.println(paper.getContent());

}publicInk getInk() {returnink;

}public voidsetInk(Ink ink) {this.ink =ink;

}publicPaper getPaper() {returnpaper;

}public voidsetPaper(Paper paper) {this.paper =paper;

}

}

6.創建測試類

public classTest {public static voidmain(String[] args) {//準備墨盒和紙張

Ink ink=newBlackInk();

Paper paper=newTextPaper();

Printer printer=newPrinter();

printer.setInk(ink);

printer.setPaper(paper);

printer.print("2222222222222222222222222222222222222222222222222222222");//輸入的文本

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541884.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541884.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541884.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java ResourceBundle getLocale()方法與示例

ResourceBundle類的getLocale()方法 (ResourceBundle Class getLocale() method) getLocale() method is available in java.util package. getLocale()方法在java.util包中可用。 getLocale() method is used to get the locale of this ResourceBundle. getLocale()方法用于獲…

centos下安裝pip時失敗:

2019獨角獸企業重金招聘Python工程師標準>>> [rootwfm ~]# yum -y install pip Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.tuna.tsinghua…

java 標準輸入流 關閉 打開_java輸出流關流疑問,以下這個程序的in和out是否要關閉?...

/**標準IOjava.lang.System類中提供以下三個靜態常量&#xff1a;staticfinalInputStreamin功能&#xff1a;“標準”輸入流&#xff0c;流已打開并準備提供輸入數據。通常&#xff0c;此流對應于鍵盤輸入或者由主機環境或用戶指.../**標準IOjava.lang.System類中提供以下三個靜…

Java RandomAccessFile readUTF()方法及示例

RandomAccessFile類readUTF()方法 (RandomAccessFile Class readUTF() method) readUTF() method is available in java.io package. readUTF()方法在java.io包中可用。 readUTF() method is used to read this RandomAccessFile as a string. readUTF()方法用于以字符串形式讀…

LCD顯示屏原理與應用

1、什么是LCD&#xff1f; (1)LCD(Liquid Crystal Display)俗稱液晶.(2)液晶是一種材料&#xff0c;液晶這種材料具有一種特點&#xff1a;可以在電信號的驅動下液晶分子進行旋轉&#xff0c;旋轉時會影響透光性&#xff0c;因此我們可以在整個液晶面板后面用白光照&#xff08…

Java PipedInputStream connect()方法與示例

PipedInputStream類的connect()方法 (PipedInputStream Class connect() method) connect() method is available in java.io package. connect()方法在java.io包中可用。 connect() method is used to cause this PipedInputStream to be connected to the given PipedOutputS…

java寫手機游戲_如何將自己編寫的JAVA小游戲寫到手機里?

2019-06-19怎么用java編寫獲取星期幾的程序&#xff1f;import java。util。*; public class WeekDay { Calendar date Calendar。getInstance(); private int getMaxDate(int moth){ moth moth -1; if(moth > 12 || moth < 0){ System。 out。println("輸入月份錯…

Java PipedInputStream receive()方法與示例

PipedInputStream類的receive()方法 (PipedInputStream Class receive() method) receive() method is available in java.io package. receive()方法在java.io包中可用。 receive() method is used to receive a byte of content and it will block when no more input remain…

java去除重復對象_Java19-2 集合類去除重復對象

List獨有方法&#xff1a;import java.util.ArrayList;import java.util.List;public class ListTest2 {public static void main(String[] args) {List listnew ArrayList();list.add("abc1");list.add("abc2");list.add("abc1");list.add(&quo…

SSM框架整合中遇到重復的問題Ambiguous handler methods mapped for HTTP

嚴重: Servlet.service() for servlet [spring] in context with path [/ssmDemo] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path /init.do: {public java.lang.String …

Java ObjectStreamClass lookup()方法與示例

ObjectStreamClass類lookup()方法 (ObjectStreamClass Class lookup() method) lookup() method is available in java.io package. lookup()方法在java.io包中可用。 lookup() method is used to lookup the descriptor for a class that can be serialized. lookup()方法用于…

java default parameter_JAVA菜鳥入門(7) default parameter , float/double vs BigDecimal

1 java的允許函數的默認參數嗎?java不支持類似C那樣&#xff0c;為函數設定默認參數&#xff0c;所以需要做的事情是&#xff0c;自己用函數重載的方式進行模擬。如下public class FFOverload {public String getName(String givenName,String familyName){return givenName&…

gitlab修改默認端口

部署gitlab的時候&#xff0c;一啟動&#xff0c;發現80和8080端口已經被占用&#xff0c;無奈&#xff0c;只得先將監聽80端口的nginx和監聽8080端口的jenkins停止。這會兒有空&#xff0c;琢磨一下如何修改gitlab的默認端口。 修改主要分為兩部分&#xff0c;一部分是gitlab總…

Java ObjectOutputStream reset()方法與示例

ObjectOutputStream類reset()方法 (ObjectOutputStream Class reset() method) reset() method is available in java.io package. reset()方法在java.io包中可用。 reset() method is used to reset this stream. It reset the stream to the position marked most recently. …

Excel 自定義關閉按鈕

遇到過這樣一個需求&#xff0c;是在excel關閉的時候&#xff0c;不要excel本身的保存窗口&#xff0c;只用自定義的. 這個的需要第一&#xff0c;是點擊關閉時候觸發&#xff0c; 第二&#xff1b;觸發后&#xff0c;不能還是彈出那個窗口 第三&#xff1a;取消后&#xff0c;…

Java OutputStreamWriter close()方法與示例

OutputStreamWriter類close()方法 (OutputStreamWriter Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to first flush before closing the stream and the method write() or flush() invok…

深入理解Netscaler INat

深入理解Netscaler INatNetscaler的INat主要是用作基于目的地址的轉換&#xff0c;將client訪問的公網IP通過Netscaler轉換成服務器的私網IP&#xff0c;與DNAT作用類似。由于Netscaler默認的工作機制就是同時做源IP&#xff1a;【源端口】目的IP&#xff1a;【目的端口】的轉換…

java 方法 示例_Java語言環境getDisplayCountry()方法與示例

java 方法 示例區域設置類getDisplayCountry()方法 (Locale Class getDisplayCountry() method) Syntax: 句法&#xff1a; public final String getDisplayCountry();public String getDisplayCountry(Locale lo);getDisplayCountry() method is available in java.util pack…

格力電器Java面試題_JAVA設計模式學習--工廠模式

今天談一下對工廠模式學習的總結。看完視頻和文章之后要自己表述工廠模式&#xff0c;總是感覺無從說起&#xff0c;不知道怎么去定義工廠模式&#xff0c;反復看了幾遍之后終于理解一點。自己理解工廠模式是通過這兩種模式的特點來理解和定義的&#xff0c;首先工廠模式有簡單…

為什么玩我的世界老提示Java se錯誤_我的世界error錯誤信息 error could解決方法

我的世界是一個及其開放的沙盒游戲&#xff0c;而在這個游戲中有不少的問題&#xff0c;比如說遇到error該如何解決呢&#xff0c;看小編給大家帶來的我的世界error錯誤的解決方法&#xff0c;希望大家喜歡。error應用程序錯誤信息。包括“Error:Unable to access jarfile mcpc…