java泛型问题
ringa_lee
ringa_lee 2017-04-17 15:06:07
0
6
244

有如下一个类:

public class BaseDao<T> {

    
    public <E> Page<E> find(Page<E> page, String qlString) {
        return find(page, qlString, null);
    }

    public T get(String id){
        
       return (T)getById(id); 
    }
}

对于get方法中的T,我能够理解,即外部传入的参数化类型
但是对于find方法中的E,表示不太理解,如果是简单的规范参数,为什么在返回类型的前面还要加一个<E>
求解惑

ringa_lee
ringa_lee

ringa_lee

reply all(6)
刘奇

The first one is a generic method (the parameter list uses one or more generic types). The generic types used in the () method parameter list are listed in front of the method.

https://docs.oracle.com/javase/tutorial/extra/generics/methods.html
http://www.cnblogs.com/iyangyuan/archive/2013/04/09/3011274.html

PHPzhong

Because this E type is not the generic type T given in the class definition

黄舟

The generic parameters of the java method are written in front of the return value. c# is written after the method name.

巴扎黑

Um, I don’t know if I understood it wrong. .
The T in get is not a canonical parameter but declares that the return type is T, right?

The page function only specifies that the parameter has page, and the page itself is of type E

小葫芦

This is a generic method, which means it receives Page whose type parameter is E and returns Page

阿神

The parameters and return value of the find method are of the type Page, but this Page is also generic. It can be run without writing, but there will be a warning

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template