Java泛型能否传递?

泛型是传递过去了,只是取className的时候有点问题。StudentDao 是继承了BaseDao,而StudentService不是,所以在BaseDao的构造方法里 getClass()各自返回不同的结果,自然getClass().getGenericSuperclass()各自返回的结果也不同,StudentDao创建时返回BaseDao\u0026lt;Student\u0026gt;,StudentService创建时返回Object。具体可以打印看看。=========================================class BaseModel implements Serializable {private static final long serialVersionUID = 1L;}class Student extends BaseModel {private static final long serialVersionUID = 1814118305976862163L;}class BaseService\u0026lt;T extends BaseModel\u0026gt; {protected BaseDao\u0026lt;T\u0026gt; baseDao;// = new BaseDao\u0026lt;T\u0026gt;(); public BaseService(T baseModel) {baseDao = new BaseDao(baseModel); }}class BaseDao\u0026lt;T extends BaseModel\u0026gt; {protected String className;protected T test;public BaseDao(T baseModel) {className = baseModel.getClass().getSimpleName(); System.out.println("className " + className);// System.out.println("getClass() "+getClass());// System.out.println("getClass().getGenericSuperclass() "+getClass().getGenericSuperclass()); }}class StudentService extends BaseService\u0026lt;Student\u0026gt; {public StudentService() {super(new Student()); }}class StudentDao extends BaseDao\u0026lt;Student\u0026gt; {public StudentDao() {super(new Student()); }}public class Test {public static void main(String args) {// 测试一 StudentDao studentDao = new StudentDao();// System.out.println(studentDao.className);// 输出Student // System.out.println(teacherDao.className);// 输出Teacher,这里和问题2相关,给出的代码中省略了Teacher的相关类 // 测试二 StudentService studentService = new StudentService();// 报错// System.out.println(studentService.className);// 输出Student }}StudentService的情况不可以用你的方式取className,只是这样而已,并不是泛型没有传递。


    推荐阅读