私のプログラムには、フィールドに同じクラスを持つフィールドを持つ無限の再帰があります。彼らはシングルトンですが、これはそれらを構築しない原因とはなりません。ところで、私はプログラムを書いたが、実際にフェイズ配列を削除することはできない。無限再帰合成クラス
abstract class Phase{
protected String phaseName;
protected char[] keys;
protected String[] commands;
protected Phase[] phases;
protected StringBuilder pattern;
}
class RemotePhase extends Phase{
private static RemotePhase remotePhase;
protected RemotePhase(){
phaseName="Remote.";
commands=new String[]{"Lock/unlock windows", "Toggle door", "Select dog menu"};
setPattern();
//Just below here starts an infinite loop
phases=new Phase[]{FixWindows.getFixWindows(), ToggleDoor.getToggleDoor(), SelectDogPhase.getSelectDogPhase()};
}
public static RemotePhase getRemotePhase(){
if(remotePhase==null){
remotePhase=new RemotePhase();
}
return remotePhase;
}
}
final class FixWindows extends Phase{
private static FixWindows windows;
private RemotePhase remotePhase;
private FixWindows(){
//execution keeps coming here as FixWindows object is never constructed
remotePhase=RemotePhase.getRemotePhase();
}
public static FixWindows getFixWindows(){
if(windows==null){
windows=new FixWindows();
}
return windows;
}
}
私はRemotePhase静的クラスを作成しようとしているとFixWindowsはメンバーのためにそれを使用しますが、私は抽象クラスの非静的メソッドをオーバーライドしようとすると、非静的コンテキストにおけるFixWindowsからそれらを呼び出そうとエラーに走りました。私はそれを静的にしたくないのですが、私はRemotePhaseを参照するために追加のクラスを作る必要があるからです。
これを行う方法はありますが、ありがとう
のための同じを使用することができ、
FixWindows
ためにこのアイデアを示しています。 – Dici'FixWindows'コンストラクタを変更し、' RemotePhase'(this)を渡して 'getFixWindows'で構築するか、' getFixWindows'で 'remotePhase'だけを設定します。他のコンストラクタでコンストラクタを呼び出さないようにしてください。あなたの静的メソッドは、そのようなものを設定するのに適しています –
それを表示することができます –