简体   繁体   中英

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. 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. Your list is specified to contain classes not instances of the OrderChange class

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> instance while you probably need the generic type to be simply ? 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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