3
 public class Service{ 
       String serviceName;
      //setter and getter
  }

  public class Version{ 
       int VersionID;
      //setter and getter
  }

  public void test(Object list){

        //it shd print the obtained list

   }

列表<服务> list1; //Service是一个Bean
List<版本> list2; //版本是一个Bean
test(list1);
测试(列表2);

现在测试方法 shd 打印获得的列表 - (即)如果列表是Service类型,那么 serviceName 应该使用它的 getter 打印。如果列表类型是版本,则应打印 versionID。
是否可以在不使用接口或抽象类的情况下实现这一点?

4

4 回答 4

0
if(Object instanceof List) {
    List list = (List)Object ;

   for(int index=0; index < list.length();index++) {
       Object obj = list.get(index);

       if(obj instanceof Service) {
         //cast to service and print value or use reflection
           Service service= (Service)obj ;
           System.out.println(service.geServiceName());
       } else if(obj instanceof Version) {
          // cast to Version and print versionID  someting
          Version version = (Version)obj ;
          System.out.println(version.getVersionId());
     }
   }
}
于 2012-06-29T14:40:26.403 回答
0
public void test(Object list) {
    if(list instanceof List)
        test((List)list);
}

public void test(List<?> list) {
    if (!list.isEmpty()) {
        Object o = list.get(0);
        if (o instanceof Version) {
            @SuppressWarnings("unchecked")
            List<Version> lVersion = (List<Version>) list;
            for (Version v : lVersion) {
                System.out.println(v.getVersionID());
            }
        } else if (o instanceof Service) {
            @SuppressWarnings("unchecked")
            List<Service> lService = (List<Service>) list;
            for (Service s : lService) {
                System.out.println(s.getServiceName());
            }
        }
    }
}
于 2012-06-29T14:41:55.293 回答
0
static public class Service {

    String serviceName;

    @Override
    public String toString() {
        return serviceName;
    }
}

static public class Version {

    String VersionID;

    @Override
    public String toString() {
        return VersionID;
    }
}

static public void test(List<?> list) {
    for (Object object : list) {
        System.out.println(object.toString());
    }
}
于 2012-06-29T14:45:43.140 回答
0

@danLeon 到目前为止有最简单的想法(添加toString到类中),假设您可以访问Serviceand Version

我不确定你为什么要考虑反射,但我唯一能想到的是你想要的东西可以与任何具有单个属性Stringgetter 的对象一起使用,然后你会做这样的事情(疯狂的 IMO,但它使用反射):

Class clazz = list.get(0).getClass();
Method[] methods = clazz.getDeclaredMethods();
Method onlyStringGetter = null;
for (Method method: methods) {
    String mName = method.getName();
    if (mName.matches("get\w+") {
        if (method.getReturnType().equals(String.class) {
            if (onlyStringGetter != null) thrown new RuntimeException("More than one String getter available");
            onlyStringGetter = method;
        }
    }
}
if (onlyStringGetter == null) throw new RuntimeException("No String getter found for class: " + clazz.getName());
List<String> strings = new ArrayList<String>();
for (Object singleStringAttribObj: list) {
    // some exception handling needed for below
    String result = (String)onlyStringGetter.invoke(singleStringAttribObj);
    strings.add(result);
}
System.out.println(strings);

我没有编译或尝试过,但这大约是正确的。肯定需要一些额外的异常处理

于 2012-06-29T15:12:35.940 回答