package com.reflect.other;
import java.io.Serializable;
/**
* 簡單pojo類(測試用,無具體意義)
*
* @author edgewalk
* @date 2017年6月5日
*/
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.reflect.other;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* 父類
*
* @author edgewalk
* @date 2017年6月5日
* @param <T>泛型,可以由之類繼承傳遞
*/
public class Parent<T> {
/**
* 用於接收傳遞的clazz
*/
private Class clazz;
public Parent() {
Class<? extends Parent> clazz = this.getClass();
ParameterizedType type = (ParameterizedType) clazz.getGenericSuperclass();
Type[] types = type.getActualTypeArguments();
clazz = (Class) types[0];
}
}
package com.reflect.other;
/**
* 子類
* @author edgewalk
* @date 2017年6月5日
*/
public class Student extends Parent<Person> {
/**
* 當我們創建本類對象的時候,會先創建一個student類的空參構造,
* 然後會返回父類的空參構造(Parent),此時父類就會獲取到子類傳遞給他的泛型,在父類中執行相關操作
*/
}