そのため、渡されるオブジェクトの代わりに、列挙型データ型をパラメーターとして使用しようとしています。単純な switch ステートメントが機能することはわかっていますが、それは私にはエレガントに思えません。検索したところ、列挙型にもアクションを関連付けることができることがわかりましたが、この場合の使用方法や、可能かどうか、または本当に疲れているかどうかははっきりしていません。コードを使用して、私が求めていることを説明してみましょう。
まず、基本的に列挙型を使用して参照しようとしている他のオブジェクトの特定のフィールドを持つクラスがあります。この場合、ツリーのフィールドの 1 つに作用するメソッドがあります。それらは複数のツリーであるため、メソッドはどのツリーに作用するかを知る必要があります。
public class bstContactManage()
{
// fields of other objects
BST searchTreeFirstName = new BST(new ComparatorObjOne);
BST searchTreeLastName = new BST(new ComparatorObjTwo);
// and so on and so forth
public boolean modify(Contact contactToFind, BST ToFindIn, String newContactInfo)
{
Contact contUpdate = new Contact(ContactToFind)//save for readdition to tree
contUpdate.update(newContactInfo);
toFindIn.remove(contactToFind);
if(toFindIn.add(contUpdate)) return true;
else return false;
}
}
私が疑問に思っていること、または多かれ少なかれ熟考しているのは、BSTパラメーターを列挙型に置き換える方法です。switchステートメントを使用できることはわかっていますが、int値を渡してそれを許可するよりも効果的ではないかもしれません。ワイルドに!
メソッドを次のように取得する方法はありますか
public boolean modify(Contact contactToFind, Enum BSTType, String newContactInfo)
{
Contact contUpdate = new Contact(ContactToFind)//save for readdition to tree
contUpdate.update(newContactInfo);
BSTType.remove(contactToFind);
if(BSTType.add(contUpdate)) return true;
else return false;
}
私の質問のほとんどは、
bstContactManage man = 新しい bstContactManage()
別のクラスでインスタンス化されるため、安全ではないか、次のようなことをするのは適切ではないようです
man.modify(contactIn, man.searchTreeFirstName, "String");
アップデート:
より明確にするために、特定のBSTを検索する別のメソッドfindがあり、現在、このように実装しています
public List<Contact> find(BinarySearchTree treeUsed, String findThis)
{
//create a new contact with all fields being the same, find is dependent and comparator on tree;
Contact tempContact = new Contact(findThis, findThis, findThis);
return treeUsed.getEntry(tempContact); // where getEntry returns a list of all matching contacts
}
私は次のようなことができます
public List<Contact> find(EnumField field, String findThis)
{
BST treeUsed;
switch(Field){
case FIRST:
treeUsed = this.searchTreeFirstName;
break;
cast LAST:
treeUsed = this.searchTreeLastName;
break;
Contact tempContact = new Contact(findThis, findThis, findThis);
return treeUsed.getEntry(tempContact); // where getEntry returns a list of all matching contacts
}