2013-08-20 11 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(); 
} 

EDIT

私は別の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クラスは最後に、私はBaseMatcherがobviouoslyすべてのMatcherが実装すべきロジックを実装しているため、これは、設計上の問題だと思い最終

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

として_dont_implement_Matcher___instead_extend_BaseMatcher_メソッドを実装します。したがって、Matcherを抽象クラスにしてテンプレートメソッドを使用する方が良いでしょう。

しかし、私はバイトコードの互換性と新機能の最適な妥協であったので、彼らはそれをしたと思います。あなたが受け入れテストのいくつかのフォームを持っていなければならないよう

public class MyMotherClass { 

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

} 

は、ほとんどの場合、これは、十分なはずです。(私はコメントで、それへの言及を見ましたが)ほとんどの人が見渡せている

4

あなたの具体的なクラスがツリーの唯一の葉になるように、あなたの階層を手直しができます。代わりに

myClass extends myMotherClass 

myClass extends myMotherAbstractClass 
myMotherClass extends myMotherAbstractClass 

抽象クラスはインスタンス化の両方のクラスによって継承される。この方法を考えてみましょう。この場合、myMotherClassは非常に薄いでしょう。実装はmyMethodです。

-1

実際にメソッドを実装するには、interfaceを使用する必要があります。いくつかのいずれかがMyClass implements MyInterfaceとして、このインターフェイスから実装する場合

public interface MyInterface{ 

    void myMethod(); 
} 

は今、あなたはあなたがメソッドをオーバーライドするサブクラスを強制することはできませんmyMethod();

public MyClass implements MyInterface{ 

    public void myMethod{ 
    // do something 
    } 

} 
+2

私はこれが問題を解決するとは思わない。 – Lokesh

0

ことの一つは、次の実装であります(たとえそれが継承クラスを手作業でテストしているとしても)。理論的には、あなたはまだ、その方法が生産されてからはじめて上回っていないことを誰も気づかない可能性を紹介しています。

関連する問題