2013-08-20 28 views
11

我想強制子類實現我母親班的實施方法。 我看這Java - Force implementation of an implemented method但我不能將我的母親班級轉換爲抽象類。如何在不使用抽象的情況下強制實現子類中的方法?

public class myMotherClass { 

    myMethod { 

     ...some code .. 

    } 

} 

public class myClass extends myMotherClass { 

    myMethod { 

     ... other code ... 
    } 

} 

所以,在這個例子中,我想強制myClass實現myMethod。

對不起,我的英語...

+1

總之:你不能沒有抽象 –

+0

@stonedsquirrel接口怎麼樣? –

+0

如果類沒有實現該方法,那麼使用註釋或拋出異常是不可能的? – Maniz

回答

16

你不能強迫一個子類覆蓋的方法。你只能強制它通過抽象來實現一個方法。

所以,如果你不能讓myMotherClass抽象的,你只能引入擴展myMotherClass和代表們必須實現該方法的另一超:

public abstract class EnforceImplementation extends myMotherClass { 

     public final void myMethod(){ 
      implementMyMethod(); 
     } 

     public abstract void implementMyMethod(); 
} 

編輯

我發現了另一個interessting方式解決問題的例子是hemcrest api由mockito使用。

public interface Matcher<T> extends SelfDescribing { 

    /** 
    * Evaluates the matcher for argument <var>item</var>. 
    * <p/> 
    * This method matches against Object, instead of the generic type T. This is 
    * because the caller of the Matcher does not know at runtime what the type is 
    * (because of type erasure with Java generics). It is down to the implementations 
    * to check the correct type. 
    * 
    * @param item the object against which the matcher is evaluated. 
    * @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>. 
    * 
    * @see BaseMatcher 
    */ 
    boolean matches(Object item); 

    /** 
    * This method simply acts a friendly reminder not to implement Matcher directly and 
    * instead extend BaseMatcher. It's easy to ignore JavaDoc, but a bit harder to ignore 
    * compile errors . 
    * 
    * @see Matcher for reasons why. 
    * @see BaseMatcher 
    */ 
    void _dont_implement_Matcher___instead_extend_BaseMatcher_(); 
} 

該接口指定方法_dont_implement_Matcher___instead_extend_BaseMatcher_。當然,這並不妨礙其他人實施Matcher界面,但它指導開發者朝着正確的方向發展。

而且BaseMatcher類實現_dont_implement_Matcher___instead_extend_BaseMatcher_方法,最終

public final void _dont_implement_Matcher___instead_extend_BaseMatcher_() { 
    // See Matcher interface for an explanation of this method. 
} 

最後,我認爲這是一個設計問題,因爲BaseMatcher obviouosly實現了每一個Matcher應該實現的邏輯。因此,將Matcher作爲抽象類並使用模板方法會更好。

但我想他們這樣做是因爲這是字節碼兼容性和新功能之間的最佳折衷。

4

讓你的具體類僅僅是樹的葉子你可以返工您的層次結構。

而不是

myClass extends myMotherClass 

考慮

myClass extends myMotherAbstractClass 
myMotherClass extends myMotherAbstractClass 

這樣的抽象類是由兩個實例化的類繼承。在這種情況下,myMotherClass可能會非常薄,只是執行myMethod

-1

如果你真的想強制執行方法使用應該使用interface

public interface MyInterface{ 

    void myMethod(); 
} 

現在,如果有人想從這個接口來實現爲MyClass implements MyInterface,你必須實現myMethod();

public MyClass implements MyInterface{ 

    public void myMethod{ 
    // do something 
    } 

} 
+2

我不認爲這會解決問題。 – Lokesh

0

有一點大多數人都俯瞰如下實現(儘管我看到一個評論它提):

public class MyMotherClass { 

    public void myMethod() { 
     throw new RuntimeException("Method not overwritten"); 
    }  

} 

在大多數情況下,這應該是不夠的,因爲你應該有某種形式的驗收測試(即使它只是手工測試繼承類)。從理論上講,你仍然在介紹這樣一種可能性,即沒有人會意識到這種方法在生產之前還沒有被過度使用。

相關問題