Java中的接口是什麼? Java 接口初學者指南。 如何使用它? 附上示例。
已發表: 2020-10-31
為什麼以及何時使用接口?
- 接口是 Java 中的引用類型。
- 它類似於類。
- 它是抽象方法的集合。
- 它用於實現完全抽象。
我相信這是您在 Java 面試中可能會想到的第一個問題。 非常基本的問題,但在面試中被廣泛使用
這個問題沒有完美的答案,有很多方法可以回答這個問題。 可能是你的面試官正在尋找這個問題的實用方法? 可能的。
那麼讓我們從Java接口的基本定義開始吧
我們將通過多個示例進行相同的處理。
- Java中的接口是什麼?
- 帶有實時示例的java中的接口是什麼?
- 為什麼在java中使用接口
- 界面設計Java
- Interface上最常見的面試問題
接口基礎:

-
interface
只是一個契約,是對實現類將具有的行為的描述。 實現類確保它將具有可以在其上使用的這些方法。 它基本上是班級必須做出的合同或承諾。 - 如果在您的項目中所有不同的實現共享相同的方法簽名怎麼辦? 在這種情況下,界面效果最好。
- 大項目實施後的後期,看看你有沒有把接口定義實現到~50個地方,如果換接口怎麼辦? 您必須對項目中的所有 50 個位置進行修改。
- 建議在設計階段花更多時間定義接口,而不是在後期更改它
- 接口由
singleton
變量(public static final
)和public abstract
方法組成。 當我們知道該做什麼但不知道該怎麼做時,我們通常更喜歡實時界面。 接口不能包含實例字段。 - 實現接口的類必須為所有存在的方法提供方法定義。
- 一個類可以實現多個接口。
- 可以將接口實現添加到任何現有的第三方類。
- 一個接口可以包含任意數量的方法。
- 在 Java 中,您不能實例化接口。
- 接口不包含任何構造函數。
- 接口不被類擴展; 它由一個類實現。
- 一個接口可以擴展多個接口。
接口示例:
提示 1。
創建接口CrunchifyDatabaseInterface.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package crunchify . com . java . tutorials ; import java . util . Map ; import java . util . UUID ; /** * @author Crunchify.com * What Is an Interface in Java? Beginners Guide to Java Interface. */ public interface CrunchifyDatabaseInterface { // Basic Database CRUD Operations // Insert boolean insertCrunchifyRecord ( UUID id , String name , String address , String phone , String zip , String comments ) ; // Delete public boolean deleteCrunchifyRecord ( UUID id ) ; // Get public Map < String , String > getListOfAllCrunchifyRecords ( ) ; // Update boolean updateCrunchifyRecord ( UUID id , Map < String , String > records ) ; } |
提示 2。
實現接口CrunchifyDatabaseOracleImpl.java
當您第一次實現接口時,Eclipse 將向您顯示添加未實現的方法。

只需單擊“ Add unimplemented methods
”,您的 IMPL 類就應該準備好使用Auto-generated method stub
。
提示 3. 實際 Impl 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package crunchify . com . java . tutorials ; import java . util . Map ; import java . util . UUID ; /** * @author Crunchify.com * What Is an Interface in Java? Beginners Guide to Java Interface. */ public class CrunchifyDatabaseOracleImpl implements CrunchifyDatabaseInterface { // Override: Indicates that a method declaration is intended to override a method declaration in a supertype. // If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold: // The method does override or implement a method declared in a supertype. // The method has a signature that is override-equivalent to that of any public method declared in Object. @Override public boolean insertCrunchifyRecord ( UUID id , String name , String address , String phone , String zip , String comments ) { // TODO Provide your actual implementation here based on your need specific to Oracle return false ; } // UUID(): A class that represents an immutable universally unique identifier (UUID). // A UUID represents a 128-bit value. // There exist different variants of these global identifiers. // The methods of this class are for manipulating the Leach-Salz variant, although the constructors allow the creation of any variant of UUID @Override public boolean deleteCrunchifyRecord ( UUID id ) { // TODO Provide your actual implementation here based on your need specific to Oracle return false ; } // Map(): An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. // This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface. @Override public Map < String , String > getListOfAllCrunchifyRecords ( ) { // TODO Provide your actual implementation here based on your need specific to Oracle return null ; } @Override public boolean updateCrunchifyRecord ( UUID id , Map < String , String > records ) { // TODO Provide your actual implementation here based on your need specific to Oracle return false ; } } |
提示 4。
類似的方式,您可以使用相同的接口來實現不同的數據庫特定操作。 比如,對於 DB2、MySQL、MongoDB、Cassandra DB 等。

下一步是什麼?
在編寫抽像類教程的過程中,然後另一個教程清楚地顯示了抽像類和接口之間的區別。