2013-06-27 115 views
2

所以,我想使用一个枚举的数据类型作为传入的对象的地方参数。我知道一个简单的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人=新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 
} 
+0

为什么'BST'(假设索引联系代表二进制搜索树)是一个'enum'? –

+0

在这种情况下,我不想让它枚举,即时只是试图有一个枚举类型,可以引用特定对象作为类中的字段。基于这样一个事实,即从不同的类调用modify()。我可以有一个int字段在修改,然后通过检查运行,说如果(1)toFindIn = suchandsuchtree;或者我可以拿枚举并创建一个switch语句,但这对我来说看起来不再符合逻辑,我查阅了更高级的枚举的使用,并且我觉得像是有一些即将丢失的东西 – chadvonburgess

回答

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 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无论是名字或姓氏

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); 
+1

某些解释会有帮助。请看[回答]? – mtk

+0

此外,您可能不希望TreeSet覆盖具有相同姓氏的Contact,您可能会发现番石榴的'Ordering.compound'有助于创建比较先订购的比较器,然后通过唯一的ID –

+0

您的回答非常有帮助,我试图让exec方法返回一个对树的引用,但遇到了我期望的静态问题,所以我想现在我将使用switch语句 – chadvonburgess