使用enum作为参数

时间:2013-06-27 12:59:15

标签: java enums

所以,我试图在传入的对象的位置使用枚举数据类型作为参数。我知道一个简单的switch语句可以工作,但这对我来说并不是很优雅。我已经搜索过并发现枚举也可以附加操作但我不太清楚如何在这种情况下使用它,或者如果它甚至可能,或者我真的很累。让我尝试使用代码来解释我在问什么。

首先,我有一个包含其他对象的某些字段的类,我基本上试图使用枚举来引用。在这种情况下,我有一个方法作用于树的一个字段,因为它们是多个树,方法需要知道要对哪个树进行操作。

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 = new bstContactManage()

将在另一个类中实例化,因此它不安全或者似乎不适合我这样做 man.modify(contactIn, man.searchTreeFirstName, "String");

更新

所以为了更清楚,我有另一个方法找到哪个搜索给定的BST,目前我正在实现它

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
}

1 个答案:

答案 0 :(得分:2)

Enum可以提供其方法的不同实现。一个很好的例子是数学运算:

enum Op {
    PLUS {
        int exec(int l, int r) { return l + r; }
    },
    MINUS {
        int exec(int l, int r) { return l - r; }
    };
    abstract int exec(int l, int r);
}

然后我可以Op.PLUS.exec(5, 7)执行5加7

有关如何使用枚举的更多详细信息,请参阅http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

在你的情况下,我不会将enum用于具有大量逻辑和状态的东西,但是这里是你如何使用具有不同实现的方法的枚举。

enum BSTType {
    SearchTreeFirstName {
        void someMethod(Contact c) {...}
    },
    SearchTreeLastName {
        void someMethod(Contact c) {...}
    };
    abstract void somemethod(Contact c);
}

public boolean modify(Contact contactToFind, BSTType bstType, String newContactInfo) {
    // ...
    bstType.someMethod(contact);
    // ...
}

通过查看变量名和类名,我认为你的实际意思是通过名字或姓氏索引TreeSet中的Contact

enum IndexType implements Comparator<Contact> {
    IndexByFirstName {
        @Override
        public int compare(Contact o1, Contact o2) {
            return o1.firstName.compareTo(o2.firstName);
        }
    },
    IndexByLastName {
        @Override
        public int compare(Contact o1, Contact o2) {
            return o1.lastName.compareTo(o2.lastName);
        }
    };
}
TreeSet<Contact> contacts = new TreeSet<Contact>(IndexType.IndexByLastName);