简体   繁体   English

带有参数“List”的Java方法 <Class<? extends Object> &gt;”

[英]Java Method with the parameter “List<Class<? extends Object>>”

I want a method that works like this, but I can't access the properties of the objects I pass in. I can only call class methods like '.getFields()', 'getSuperType()', etc. All objects that are subclasses of OrderChange have an rph field, and I need their value. 我想要一个像这样工作的方法,但我无法访问我传入的对象的属性。我只能调用类方法,如'.getFields()','getSuperType()'等。所有对象都是OrderChange的子类有一个rph字段,我需要它们的值。 How do I do this? 我该怎么做呢?

private boolean validateOrderChange(List<Class<? extends OrderChange>> input, boolean verifyOnly) {
    for (Class<? extends OrderChange> orderChange : input) {
        if (StringUtil.isNullOrEmpty(orderChange.getRph())) {
            return false;
        }
    }
}

The way I see it, you don't have Class type in the list. 我看到它的方式,你不必Class列表中的类型。 Your list is specified to contain classes not instances of the OrderChange class 您的列表被指定为包含而不是OrderChange类的实例

private boolean validateOrderChange(List<? extends OrderChange>> input, boolean verifyOnly) {
    for (OrderChange orderChange : input) {
        if (StringUtil.isNullOrEmpty(orderChange.getRph())) {
            return false;
        }
    }
}

The problem with your code is that you are defining the generic parameter as a Class<? extends OrderChange> 代码的问题在于您将泛型参数定义为Class<? extends OrderChange> Class<? extends OrderChange> instance while you probably need the generic type to be simply ? extends OrderChange Class<? extends OrderChange>实例,而您可能需要简单的泛型类型? extends OrderChange ? extends OrderChange . ? extends OrderChange

The difference between the two is that the one you used holds a reference to a data structure containing the information about the class that are, for instance, used by reflection. 两者之间的区别在于您使用的是对包含有关类的信息的数据结构的引用,例如,反射使用的类。

On the opposite, what you need is a reference that can hold any of the subclasses of OrderChange . 相反,您需要的是一个可以包含OrderChange任何子类的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM