抽象メソッドを持つ抽象クラスを持つライブラリがあるとします。
public abstract class MyAbstractClass{
public void myMethod(){
int a = doSomething("hi");
}
public abstract void doSomething(String param);
}
ここで、メソッドにパラメーターを追加することにしましたが、古いコードを使用できるように、古いメソッドの機能を維持したいと思います。
public void myMethod(){
int a = ?
}
/**
* @deprecated use doSomething(String, String) instead.
*/
@Deprecated
public int doSomething(String param){ return doSomething(param, null); }
public abstract int doSomething(String param, String secondParam);
myMethod
このシナリオで自分をどのように実装しますか?
AndroidサポートライブラリのPagerAdapter
クラスには、実際にはこのような構造がありますが、その逆です。
public Object instantiateItem(ViewGroup container, int position) {
return instantiateItem((View) container, position);
}
/**
* @deprecated Use {@link #instantiateItem(ViewGroup, int)}
*/
public Object instantiateItem(View container, int position) {
throw new UnsupportedOperationException(
"Required method instantiateItem was not overridden");
}
この振る舞いはやめるべきですか?そして、この構造を使用する場合、どのメソッドを呼び出すかをどのように知ることができますか?