見た目よりも複雑ですが、私はそのようなことを試みる義務があると思います. 列挙型のプロトタイピングを使用して抽象親クラスを作成したい (おそらく 1 つの値のみで列挙型を宣言し、それがデフォルトのユニット化されたものになり、サブクラスから使用するいくつかのメソッドを宣言したい)、次に、抽象親を拡張して実際にまったく同じ列挙型を初期化するクラスを作成したい (これにより、親列挙型が実質的に非表示になることがわかっています)。
私はこのレベルの抽象化についてあまり知らないので、より実用的な解決策がある場合に備えて、問題の性質について説明します。列挙型に基づいて多くのコマンドを実装するクラスを含むファイルがたくさんあります。(例えば、class1 はオブザーバーを実装し、どのコマンドが選択されたかを決定するために enum ベースのスイッチを使用する update メソッドを持っています。他のクラスにも同じことが当てはまります)すべてのクラス (CommandSet など) で同じ名前を使用して、列挙型の内部メソッドを使用してヘルプ リストをシステムに出力できる汎用メソッドを親内に配置できるようにします。これで、すべてのクラスでまったく同じメソッドを書き直すことができることがわかりましたが、作成中のライブラリを他の人が拡張し続けることができるように、メソッドを抽象化したいのです!
うまくいけば、私が混乱しすぎたり、混乱しすぎたりせず、誰かが助けてくれることを願っています! :)
編集:コードのアイデアは次のとおりです(おそらく正しくありません):
public abstract class Commands{
enum CommandSet{
// empty command, placeholder
null_command ("command name", "command description");
// the Strings used for name and description
private final String name;
private final String description;
// constructor
CommandSet(String name, String description){
this.name=name;
this.description=description;
}
// get parameters
public String getName(){
return name;
}
public String getDescription(){
return description;
}
}
public void showHelp(){
for (CommandSet i : CommandSet.values()) {
printf(i.getName(),":",i.getDescription());
}
}
}
public class StandardCommads extends Commands implements Observer{
// I want to change the enum here, just changing the values so that null_command ("command name", "command description") will get removed and I will add a dozen other values, but keep the methods that the parent had
// update inherited from Observer
@Override
public void update(Observable observable, Object object) {
// I want the commands inside the switch cases defined inside this class's enum
switch(CommandSet.valueOf(String.valueOf(object)){
case command1: doStuff1();break;
case command2: doStuff2();break;
...
case commandN: doStuffN();break;
}
// other methods
void doStuff1(){
...
}
...
void doStuffN(){
...
}
}
public class NonStandardCommads extends Commands implements Observer{
// Another set of commands here for the enum keeping the same methods it had in the parent
// update inherited from Observer
@Override
public void update(Observable observable, Object object) {
// Other set of commands inside this class used in the switch statement
switch(CommandSet.valueOf(String.valueOf(object)){
case Zcommand1: doStuffz1();break;
case Zcommand2: doStuffz2();break;
...
case ZcommandN: doStuffzN();break;
}
// other methods
void doStuffz1(){
...
}
...
void doStuffzN(){
...
}
}