2012-11-19 72 views
-1

我正在學習Java接口和方法編寫。我想知道你將如何創建一個使用這個接口的類? 什麼是最好的方法來實現它包括的方法?目前,我正在努力學習寫作方法的不同方法。 在此先感謝您的幫助:)實現Java接口和方法

interface Dealer { 

    void assignPlayers(ArrayList<Player> p); 

    ... 

    public void settleBets(); 
} 

回答

3

很簡單:

public class BakratDealer implements Dealer { 
    // implement all the methods here 
} 

這不是關於最佳方式;這是只有的方式。您必須實現接口中的所有方法或聲明類abstract

該接口不需要使用關鍵字public;接口中的所有方法默認都是公共的。

+1

並且爲了增加,界面中的所有變量都是常量。 :) – PermGenError

+1

公共靜態最後,準確地說。 – duffymo

+0

這是恆定.. ..:P – PermGenError

1

使用implements在你的類declartion關鍵字來實現一個接口

public class YourClass implements Dealer { 

//implement all your method defined in the interface here 
} 
0

您可以實現類,你繼承另一個類的方法相同,除非你用的是「工具」,而不是「延伸」。

示例 - 公共類識別TestClass實現經銷商{}

在正在使用的實現類的,你寫的方法就像你寫的任何其他方法。您只需確保從接口實現所有方法,或者該類必須是抽象類。