Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 理解泛型方法返回类型_Java_Jpa_Generics - Fatal编程技术网

Java 理解泛型方法返回类型

Java 理解泛型方法返回类型,java,jpa,generics,Java,Jpa,Generics,在公共接口路径扩展表达式中,有一种方法是: <Y> Path<Y> get(String attributeName); 因为当我将“vincent”传递给这个方法时,标识符Y被确认 get方法让我对使用generic感到困惑,我不知道如何使此方法工作或返回有用的内容。您将类声明为generic,将方法声明为generic,但两者都使用Y作为类型参数的标识符。您需要从get方法中删除,一切都会好起来: class abc<Y> implements myin

公共接口路径扩展表达式中,有一种方法是:

<Y> Path<Y> get(String attributeName);
因为当我将“vincent”传递给这个方法时,标识符Y被确认


get方法让我对使用generic感到困惑,我不知道如何使此方法工作或返回有用的内容。

您将类声明为generic,将方法声明为generic,但两者都使用
Y
作为类型参数的标识符。您需要从
get
方法中删除
,一切都会好起来:

class abc<Y> implements myinterfa<Y> {
    Y ab;

    public Y get(String attributeName) {
        return ab;
    }
}

你的问题不清楚。什么是路径
。您如何调用
get
使用
abc
而不是
abc
或从类声明中调用@YassinHajaj您不能这样做,因为
Y
也用于
实现myinterfa
您可能应该注意,此签名:
static Path methodName(){}
通常应静态使用。如果它是一个类的成员,则删除
static
,并确保泛型符号与类定义中的符号匹配,例如
Path methodName(){}
,其中
ClassName
@Michael我的意思是来自
abc
部分。我们可以这样做,通常情况下,
类abc实现myinterfa
@YassinHajaj接口的泛型类型符号必须存在并首先为类定义。您将得到一个符号Y不存在的编译错误。
  @Override
    public List<WebSite> list(WebSite webSite, Integer page, Integer pageSize) {
        Pageable pageable = new PageRequest(page, pageSize, Sort.Direction.ASC, "id");
        Page<WebSite> pageWebSite = webSiteRepository.findAll(new Specification<WebSite>() {

            @Override
            public Predicate toPredicate(Root<WebSite> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (webSite != null) {
                    if (StringUtil.isNotEmpty(webSite.getName())) {
                        predicate.getExpressions().add(cb.like(root.get("name"), "%" + webSite.getName().trim() + "%"));
                    }
                    if (StringUtil.isNotEmpty(webSite.getUrl())) {
                        predicate.getExpressions().add(cb.like(root.get("url"), "%" + webSite.getUrl().trim() + "%"));
                    }
                }
                return predicate;
            }
        }, pageable);
        return pageWebSite.getContent();
    }
public interface WebSiteRepository extends JpaRepository<WebSite, Integer>, JpaSpecificationExecutor<WebSite> {

}
public interface Path<X> extends Expression<X> {
    <Y> Path<Y> get(String attributeName);
}
<Y> Path<Y> get(Y attributeName);
String result = path.get("vincent");
class abc<Y> implements myinterfa<Y> {
    Y ab;

    public Y get(String attributeName) {
        return ab;
    }
}
class abc<Y> implements myinterfa<Y> {
    public <T> T get(String attributeName) {
        return null; /* or something useful... */
    }
}