类型Validator< capture#2-of?的方法validate(capture#2-of扩展对象)扩展Object>不适用于参数(字符串) [英] The method validate(capture#2-of ? extends Object) in the type Validator<capture#2-of ? extends Object> is not applicable for the arguments (String)

查看:178
本文介绍了类型Validator< capture#2-of?的方法validate(capture#2-of扩展对象)扩展Object>不适用于参数(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉直接将错误作为标题,但找不到更好的标题.

I am sorry about putting the error directly as the title, but I couldn't find any better title.

我有一个定义如下的接口,可用作我所有验证程序类的蓝图:

I have an interface defined as following to be used as a blueprint for all my validator classes:

public interface Validator<T> {
    public boolean validate(T item);
}

然后我有一些可以实现它的类,可以说其中之一是这样的:

And then I have some classes that would implement it, lets say one of them is this:

public class EmptyStringValidator implements Validator<String> {
    private final String _errorMessage;

    public EmptyStringValidator() {
            this("String cannot be empty.");
    }

    public EmptyStringValidator(String message) {
            this._errorMessage = message;
    }

    @Override
    public String getMessage() {
            return this._errorMessage;
    }

    @Override
    public boolean validate(String item) {
            return gsi.application.core.Validation.isEmptyString(item);
    }

}

我想将它们全部放入一个数组中,并在一个循环中将其全部调用.这就是我正在使用的代码:

I would like to put it all in an array and call it all in one loop. So this is the code I am using:

public List<Validator<? extends Object>> validators;

public FormItem<T> addValidator(Validator<? extends Object> validator) {
    this.validators.add(validator);
    return this;
}

public boolean validate() {
    for (Validator<? extends Object> validator : this.validators)
        if (!validator.validate(this.getInputValue())) {
            this._errorMessage = validator.getMessage();
            return false;
        }
    return true;
}

但是,该代码在validate()函数(特别是在此部分)上给出了错误:

However, that code is giving an error at the validate() function, specifically at this part:

validator.validate(this.getInputValue())

它给了我我提到的错误

The method validate(capture#2-of ? extends Object) in the type Validator<capture#2-of ? extends Object> is not applicable for the arguments (String)

据我所知,这没有任何意义.据我了解,<? extends Object>应该接受从Object类派生的任何内容,对吧?

which to my understanding doesn't makes sense. To my understanding <? extends Object> should accept anything that derives from the Object class, right?

有人可以指出我做错了什么还是将我指向正确的方向? 谢谢.

Could anyone point out what am I doing wrong or point me at the right direction? Thanks.

推荐答案

顺便说一句,? extends Object与说?没什么不同.但是,这不是问题的根源.

As an aside, ? extends Object is no different from saying ?. That isn't the root of your problem, however.

问题在于validatorsList<Validator<? extends Object>>.换句话说,每个元素可以是任何类型的Validator<T>,不一定是Validator<String>.因此,您可以将Validator<String>放入其中,但是当您拉出一个元素时,您不知道它是哪种Validator,因此您不知道它是否与.

The issue is that validators is a List<Validator<? extends Object>>. In other words, each element can be any kind of Validator<T>, not necessarily a Validator<String>. So you can put a Validator<String> into it, but when you pull an element out you don't know what kind of Validator it is, and so you don't know if it is compatible with the type returned by this.getInputValue().

最简单的解决方法是为type参数指定一种具体的类型(例如:String).更复杂的解决方法是使用类型变量代替? extends Object,并让getInputValue()的类型签名使用相同的类型变量.您需要限制类型,以使getInputValue()的返回类型可分配给validate()的参数.

The simplest fix would be to specify a concrete type (eg: String) for the type parameter. A more complicated fix would be to use a type variable in place of ? extends Object, and have getInputValue()'s type signature use that same type variable. You need to constrain the types such that getInputValue()'s return type is assignable to the parameter of validate().

这篇关于类型Validator&lt; capture#2-of?的方法validate(capture#2-of扩展对象)扩展Object&gt;不适用于参数(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆