簡體   English   中英

如何確保接口實現擴展特定類?

[英]How can I ensure that an interface implementation extends a particular class?

我有兩種類型的編輯器。 一個是JTextArea的子類,一個是JTable的子類( JTextAreaJTable都是JComponent子類)。 我想要我的兩個類TextAreaEditorTableEditor來實現接口Editor ,它只有public String getText()

我希望客戶端代碼只使用Editor界面。 問題是,我的所有Editor使用JComponent方法,比如setEnabled(bool) 由於我的編輯器是一個接口,我無法擴展JComponent,因此在調用這些方法時我必須使用實現而不是接口。 所以我認為不是使用接口,而是簡單地將Editor作為JComponent的子類,並讓我的類擴展它。 問題是像TextAreaEditor這樣的類已經像JTextArea那樣擴展了一些類,所以我不能讓它們擴展另一個類。

有沒有辦法確保我的Editor類是一個JComponent,我的具體編輯器類是JComponent Editor和子類?

如果在編輯器界面的子類中公開您關心的JComponent方法,那么它們將由您的類“追溯”實現。

以下是一些用於演示該想法的代碼:

interface Editor { 
  String getText(); 
}

interface SwingEditor extends Editor { 
  void setEnabled(bool); // has to match *exactly* the signature from JComponent
}

class TableEditor extends JTable implements SwingEditor {
   // implement your getText(), and anything else you need 
   // no need to implement setEnabled, as it is provided by JTable
}

SwingEditor te = new TableEditor();
te.setEnabled(true); // will call JComponent's method

我假設你真的需要繼承這里,通常組合是Swing UI代碼的更好選擇。

使用組合而不是遺傳 大部分時間繼承都不是正確的解決方案,在您的情況下,它將使您免於編寫相當多的代碼。

TextAreaEditorTableEditor都有他們需要的JComponent實例。 將所需的所有方法添加到接口,然后將這些調用委托給JComponet

例如:

public class TextAreaEditor implements Editor {
     private final JTextArea textArea = new JTextArea();

     public void setEnabled(bool isEnabled) {
          return textArea.setEnabled(isEnabled);
     }

     //... your own methods plus other methods from JComponent
}

您可能希望獲得更多花哨並使用某種依賴注入來實例化JComponent ,但這不是必需的。 但是,如果所有更改都是您需要注入的特定JComponent ,它可以解決必須擁有類的問題。

如果您需要更多說明,請告訴我。

如果你真的需要你的Editor類成為JComponent ,你可以選擇簡單地記錄它並在你的代碼中執行強制轉換。 不是最簡潔的解決方案,但到目前為止最簡單。

另一種方法是在Editor界面中添加一個額外的方法:

public JComponent getComponent();

您的Editor實例可以通過簡單地返回this來實現此方法。

后一種方法的好處是你可以使用所有JComponent方法,而不必在你的界面中復制它們,並在2個月內得出結論,你忘了將一個JComponent方法添加到你的界面

以下設計負責:

public class TextAreaEditor extends JTextArea implements Editor {
//provide your implementation of getText()
//setEnabled(bool) doesn't have be implemented as JComponent will provide
}

TextAreaEditor te = new TextAreaEditor();
te.setEnabled(true); // will call JComponent's impl

根據上面的代碼,您自己的編輯器(TextAreaEditor)的具體實現是JComponent的編輯器和子類。

我可能會創建一個新表和textarea類來擴展它們各自的超類,並實現這樣的編輯器界面

public class JTextAreaEditor extends JTextArea implements Editor {
...
}

然后使用composition來公開Editor接口的方法

    public class JTextAreaEditor extends JTextArea implements Editor {
       private Editor editor;

       public String getValue() {
          return editor.getValue();
       }
       ...
    }

我認為你的論點是不正確的:對於一個接口(編輯器),你不應該關心如何實現這些實現。 你說你所有的編輯器實現都需要是JComponent。 這就是現在發生的事情,但它永遠不需要成為“編輯”的“要求”。 只要實現符合Editor要求的內容(getText()),Editor的設計就沒有理由強加於此。

你所談論的主要是“普通”編輯器實現的默認基類。 再次注意,只要它們符合您的編輯器界面,是否可以選擇使用此基類的實現。

public interface Editor {
    String getText();
}

public abstract class JComponentEditor extends JComponent 
        implements Editor {
    //.....
}

public TextAreaEditor extends JComponentEditor {
    public String getText() {
        // implements TextAreaEditor's version of getText
    }
}

編輯:我想我對OP的問題有點誤解了。 無論如何,我的答案中的主要論點仍然存在:強制執行“......我的編輯器類是JComponent,而我的具體編輯器類是JComponent的編輯器和子類”是沒有意義的。 它只是Editor的實現細節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM