我有以下代码:
...
if (...) { // Handling emails
int emailType = -1;
if (types != null) {
String type = foo(types);
if (type.equals("work")) {
emailType = Email.TYPE_WORK; // android.provider.ContactsContract.CommonDataKinds.Email
} else if (type.equals("home")) {
veemailType = Email.TYPE_HOME;
} else if (type.equals("mobile")) {
emailType = Email.TYPE_MOBILE;
} else if (type.equals("other")) {
emailType = Email.TYPE_OTHER;
}
bar(emailType);
}
} else if (...) { // Handling phones
int telType = -1;
if (types != null) {
String type = foo(types);
if (type.equals("work")) {
telType = Phone.TYPE_WORK; // android.provider.ContactsContract.CommonDataKinds.Phone
} else if (type.equals("home")) {
telType = Phone.TYPE_HOME;
} else if (type.equals("mobile")) {
telType = Phone.TYPE_MOBILE;
} else if (type.equals("other")) {
telType = Phone.TYPE_OTHER;
}
bar(telType);
}
} else if ...
显然我应该使用一个函数来包装这些类似的处理逻辑,但不知道该怎么做。
在C/C++
这种情况下我更喜欢宏,但看起来Java中没有宏?