[Java Generics] get T.Class from T
/**
* Copyright (c) 2010 IBOBO Corporation. All Rights Reserved.
*/
package com.ibm.util.dao.hib;
/**
* [Java Generics] get T.Class from <T>
*
* @author <a href="mailto:libo214@gmail.com">libo</a>
* @version V1.0
* @date May 11, 2010
*/
public class GenericsTClass {
public static void main(String[] args) {
//
// Output: Generics entity class Initialized: class java.lang.String
new GenericsTClass1();
//
// Throw an exception: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl
new GenericsTClass2<String>();
//
// Throw an exception: java.lang.ClassCastException: java.lang.Class
new SuperGenericsClass<String>();
}
}
class GenericsTClass1 extends SuperGenericsClass<String> {
}
class GenericsTClass2<T> extends SuperGenericsClass<T> {
}
class SuperGenericsClass<T> {
private final Class<T> entityClass;
@SuppressWarnings("unchecked")
public SuperGenericsClass() {
this.entityClass = (Class<T>)
((java.lang.reflect.ParameterizedType)
this.getClass().getGenericSuperclass()).getActua
Ïà¹ØÎĵµ£º
×÷Õß: ØýÃû, ¡¡³ö´¦:ITר¼ÒÍø,¡¡ÔðÈαà¼: лåûåû,¡¡2010-05-10 13:00
Java °ÑÄÚ´æ»®·Ö³ÉÁ½ÖÖ£ºÒ»ÖÖÊÇÕ»Äڴ棬ÁíÒ»ÖÖÊǶÑÄÚ´æ¡£ÔÚº¯ÊýÖж¨ÒåµÄһЩ»ù±¾ÀàÐ͵ıäÁ¿ºÍ¶ÔÏóµÄÒýÓñäÁ¿¶¼ÊÇÔÚº¯ÊýµÄÕ»ÄÚ´æÖзÖÅ䣬µ±ÔÚÒ»¶Î´úÂë¿é¶¨ÒåÒ»¸ö±äÁ¿Ê±£¬Java ¾ÍÔÚÕ»ÖÐΪÕâ¸ö±äÁ¿·ÖÅäÄÚ´æ¿Õ¼ä£¬µ±³¬¹ý±äÁ¿µÄ×÷ÓÃÓòºó£¬Java »á×Ô¶¯Êͷŵô ......
1. ¼òµ¥¹¤³§:
(1). ´´½¨Êä³ö(Output)½Ó¿Ú
package stone;
public interface Output {
public int MAX_COUNT =10; // ×î¶à¿É´òÓ¡¶àÉÙÌõ¼Ç¼
public void show(); ......
Ò»¸ö¼òµ¥µÄË«»÷¾ÍÄÜÔËÐÐjarÎļþµÄ´ò°ü¹ý³Ì:
http://blog.csdn.net/ihc523/archive/2009/10/11/4636832.aspx
eclipseµ¼³öjarÎļþÔÙ½«Ëüת»»³Éexe¿ÉÖ´ÐÐÎļþ:
http://hi.baidu.com/%C4%CF%BA%BD%CC%EC%CF%C2/blog/item/1601a381ac9af9d1bd3e1e3d.html
´òJar°üµÄÒ»¸öEclipse²å¼þFatJar:
http://blog.csdn.net/lqsmn613/ar ......
import java.util.LinkedList;
import java.util.List;
public class ShortestPaths {
private static String showPath[] = { "", "", "", "", "", "" };
& ......
¹Ø¼ü×Ö: µ×²ãÊý¾Ý Ãû´Ê½âÊÍ£º ²¹Â룺1 ÔÚ¼ÆËã»úϵͳÖУ¬ÊýÖµÒ»ÂÉÓв¹ÂëÀ´±íʾ(´æ´¢). ʹÓò¹Âë,¿ÉÒÔ½«·ûºÅλºÍÆäËûλͳһ´¦Àí;ͬʱ,¼õ·¨Ò²¿É°´¼Ó·¨À´´¦Àí.ÁíÍâ,Á½¸öÓò¹Âë±íʾµÄÊý¾ÝÏà¼Óʱºò,Èç¹û×î¸ßλ(·ûºÅλ)Óнøλ,Ôò½øÎ¥±»ÉáÆú. 2 ²¹ÂëÓëÔÂëµÄת»»¹ý³Ì¼¸ºõÊÇÏàͬµÄ ÊýÖµµÄ²¹Âë±íʾҲ·ÖÁ½ÖÖÇé¿ö£º £¨1£©ÕýÊýµÄ²¹Âë:ÓëÔ ......