我正在尝试重用作用于一个类对象上的代码部分,我有多个类,这些类大多数具有90%相同的方法名,现在我有一堆if else语句,其中第一行是唯一,其余的都一样,有没有办法让我传递对象并使用其方法?
ive尝试仅将重复的代码放入活动中的方法中,但是无论我如何重新初始化对象,方法名称都永远不会被识别
例如,我将创建一个名为lookupclass(Object lookupObject)的方法,并将非特定代码粘贴到其中,就在该代码我尝试通过传递的对象创建对象的各种尝试之前,大多数都会在代码中识别出新对象,但是所有方法(例如lookup.loadClass(fileToLookup))
都显示为无法识别
if (databaseFolder.equals(CustomerFolder)) {
CustomerClass lookup = new CustomerClass();
//region nonspecific code on object
lookup.loadClass(fileToLookup);
String[] tempString = new String[lookup.positionIds.length * 2];
String[] tempExpandString = new String[lookup.positionIds.length];
String[] classData = lookup.getClassData();
String[] positionNames = lookup.positionNames;
for (int i = 0; i < lookup.positionIds.length; i++) {
tempString[i * 2] = positionNames[i];
if (lookup.positionExpandableToMultipleBoolean[i] && classData[i] != null && classData[i].contains(",")) {
tempString[i * 2 + 1] = "Multiple- Click to Expand";
tempExpandString[i] = classData[i];
} else if (lookup.positionExpandableToMultipleBoolean[i]) {
tempString[i * 2 + 1] = lookup.expandablePositionIdToName(i, classData[i]);
tempExpandString[i] = classData[i];
} else if(classData[i] == null){
tempString[i * 2 + 1] = " ";
} else
tempString[i * 2 + 1] = classData[i];
}
list = tempString;
filesToExpand = tempExpandString;
//endregion
} else if (databaseFolder.equals(AddressFolder)) {
AddressClass lookup = new AddressClass();
//region nonspecific code on object
lookup.loadClass(fileToLookup);
String[] tempString = new String[lookup.positionIds.length * 2];
String[] tempExpandString = new String[lookup.positionIds.length];
String[] classData = lookup.getClassData();
String[] positionNames = lookup.positionNames;
for (int i = 0; i < lookup.positionIds.length; i++) {
tempString[i * 2] = positionNames[i];
if (lookup.positionExpandableToMultipleBoolean[i] && classData[i] != null && classData[i].contains(",")) {
tempString[i * 2 + 1] = "Multiple- Click to Expand";
tempExpandString[i] = classData[i];
} else if (lookup.positionExpandableToMultipleBoolean[i]) {
tempString[i * 2 + 1] = lookup.expandablePositionIdToName(i, classData[i]);
tempExpandString[i] = classData[i];
} else if(classData[i] == null){
tempString[i * 2 + 1] = " ";
} else
tempString[i * 2 + 1] = classData[i];
}
list = tempString;
filesToExpand = tempExpandString;
//endregion
}
答案 0 :(得分:1)
执行此操作的好方法是通过接口。您可以定义一个名为Lookup的接口,该接口具有定义的通用方法签名。然后让您的CustomerClass,AddressClass等实现该接口。这将使您可以执行以下操作:
Lookup lookup = null;
if (databaseFolder.equals(CustomerFolder)) {
lookup = new CustomerClass();
} else {
lookup = new AddressClass();
}
lookup.loadClass(fileToLookup);
//Rest of common code
答案 1 :(得分:1)
安德鲁·阿里亚斯(Andrew Arias), 谢谢你,这对我来说很好,我使所有类似的类都实现了我需要的所有方法的接口,而不仅仅是将通用代码保留在其下面,我能够将其发送给具有下面代码的方法,一些尝试和错误认识到,一旦我有了接口设置,我就可以将对象转换为方法中的新对象,这在我不断地修改几个代码块的过程中非常有用,因为我可以解决某些情况下可能产生的错误
//主代码
if (databaseFolder.equals(CustomerFolder)) {
CustomerClass lookup = new CustomerClass();
try {
lookupClass(lookup);
} catch (ClassNotFoundException e) {Log.d(AppName,e.toString()); }catch (IllegalAccessException e) {Log.d(AppName,e.toString()); } catch (java.lang.InstantiationException e) { Log.d(AppName,e.toString()); }
//方法
public void lookupClass(Object test) throws ClassNotFoundException, IllegalAccessException, java.lang.InstantiationException {
ClassInterface lookup = (ClassInterface) test;
//region nonspecific code on object
lookup.loadClass(fileToLookup);
String[] tempString = new String[lookup.positionIds.length * 2];
String[] tempExpandString = new String[lookup.positionIds.length];
String[] classData = lookup.getClassData();
String[] positionNames = lookup.positionNames;
for (int i = 0; i < lookup.positionIds.length; i++) {
tempString[i * 2] = positionNames[i];
if (lookup.positionExpandableToMultipleBoolean[i] && classData[i] != null && classData[i].contains(",")) {
tempString[i * 2 + 1] = "Multiple- Click to Expand";
tempExpandString[i] = classData[i];
} else if (lookup.positionExpandableToMultipleBoolean[i]) {
tempString[i * 2 + 1] = lookup.expandablePositionIdToName(i, classData[i]);
tempExpandString[i] = classData[i];
} else if(classData[i] == null){
tempString[i * 2 + 1] = " ";
} else
tempString[i * 2 + 1] = classData[i];
}
list = tempString;
filesToExpand = tempExpandString;
//endregion
}