繰り返し可能なコードをできるだけ基本クラスに移動したいのですが、派生クラスから基本クラスに変数を渡す必要があることに気付きました。
myClass
パラメータのない初期化メソッドが必要で、 によって「セットアップ」されmyInterface.setup()
ます。
myBase
毎回設定されていることを確認せずに、エクステンダーに変数を設定させるにはどうすればよいですかsomeBaseMethod()
interface myInterface
{
void setup(String myStr);
void someBaseMethod();
}
abstract class myBase implements myInterface
{
String myStr = null;
protected abstract String getMyStr();
// called lots of times.
public void someBaseMethod()
{
if (myStr == null) myStr = getMyStr();
// ...
}
}
class myClass extends myBase
{
String s = null;
public void setup(String myStr)
{
s = myStr;
}
protected String getMyStr()
{
return s;
}
}