62,616
社区成员
发帖
与我相关
我的任务
分享
public class SerializeUtil<T> {
public String createKey(Object id){
return "Key:" + T.class.getName() + ":" + id.toString();
}
public byte[] serialize(T obj){
// ......
}
public T unserialize(byte[] data){
// ......
}
}
public class Test {
public static void main(String[] args){
Employee emp = ......
SerializeUtil<Employee> su = new SerializeUtil<Employee>();
String strKey = su.createKey(emp.getId());
System.out.println(strKey);
}
}
package test;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
class Engineer {
}
public class TestT <T>{
private Class<T> entityClass;
public String createKey(Object keyValue){
return "Key:" + entityClass.getName() + ":" + keyValue.toString();
}
public TestT(){
try{
Type type = this.getClass().getGenericSuperclass();
ParameterizedType ptype=(ParameterizedType)type;
Type[] types = ptype.getActualTypeArguments();
if(types.length > 0)
entityClass = (Class<T>)types[0];
for(Type t : types){
System.out.println(t.toString());
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
TestT<Engineer> obj = new TestT<Engineer>();
String strKey = obj.createKey(200);
System.out.println(strKey);
}
}