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,只是这样而已,并不是泛型没有传递。
推荐阅读
- 汽车知识|押宝全新造型,东风雪铁龙新C5能否成为神龙公司“救世主”
- 中国版Evan能火吗美国9岁男童录制评测玩具视频年入800万,中国能否复制成功
- dart这编程语言现在发展怎么样了,语法与Java,c#很相似,甚至更简洁
- 汽车市场|中期改款再战D级车市场,新雷克萨斯LS能否超过宝马7系等对手?
- 澎湃汽车圈|试驾名爵MG领航 运动性和实用性能否兼顾?
- 天天汽车|超高性价比实力宠粉,2021款奕歌能否点燃你的心?
- 能否推荐一个能用快播看电影且电影都是中英双字幕的网站
- Java工程师和C++工程师在工作上有啥区别哪个更适合自身发展
- 汽车|定位更细分SUV市场,领克06能否扩大领克的“朋友圈”?
- 汽车白话|看脸蛋、观腰身、品内饰长安欧尚X5“塑光美学”能否打动挑剔的你
