ÉîÈëjava·´Éä
·´ÉäµÄ»ùʯ——ClassÀà
Java³ÌÐòÖеĸ÷¸öJavaÀàÊôÓÚͬһÀàÊÂÎÃèÊöÕâÀàÊÂÎïµÄJavaÀàÃû¾ÍÊÇClass¡£±ÈÈ磺ÖÚ¶àµÄÈË¿ÉÒÔÓÃÒ»¸öPersonÀàÀ´±íʾ£¬¶øÕâ¸öÀà¾Í¿ÉÒÔÃèÊöÈ˵ÄÐÅÏ¢(ÐÕÃû¡¢ÄêÁä¡¢ÐÔ±ðµÈµÈ)£¬¶øÖÚ¶àµÄJavaÀà¾Í¿ÉÒÔÓÃClassÀ´±íʾ¡£
Question£ºPersonÀà´ú±íÈË£¬ËüµÄʵÀý¶ÔÏó¿ÉÒÔÊÇÕÅÈý¡¢ÀîËÄ£¬ClassÀà´ú±íJavaÀ࣬ËüµÄ¸÷¸öʵÀý¶ÔÏóÓзֱð´ú±íʲôÄØ£¿
Answer: ¶ÔÓ¦¸÷¸öÀàÔÚÄÚ´æÖеÄ×Ö½ÚÂë¡£Ò»¸öÀà±»Àà¼ÓÔØÆ÷¼ÓÔص½ÄÚ´æÖУ¬Õ¼ÓÃÒ»·Ý´æ´¢¿Õ¼ä£¬Õâ¸ö¿Õ¼äÀïÃæµÄÄÚÈݾÍÊÇÀàµÄ×Ö½ÚÂ룬²»Í¬µÄÀàµÄ×Ö½ÚÂëÊDz»Í¬µÄ£¬ËùÒÔËüÃÇÔÚÄÚ´æÖеÄÄÚÈÝÊDz»Í¬µÄ£¬ÕâÒ»¸ö¸ö¿Õ¼ä¿É·Ö±ðÓÃÒ»¸ö¸öµÄ¶ÔÏóÀ´±íʾ£¬ÕâЩ¶ÔÏóÏÔÈ»¾ßÓÐÏàͬµÄÀàÐÍ£¬¼´ClassÀàÐÍ¡£
ÈçºÎµÃµ½¸÷¸ö×Ö½ÚÂë¶ÔÓ¦µÄʵÀý¶ÔÏó,ÓÐÈýÖÖ·½·¨£º
1¡¢ÀàÃû.class,ÀýÈç:System.class
2¡¢¶ÔÏó.getClass(),ÀýÈç:new Data().getClass()
3¡¢Class.forName("ÀàÃû"),ÀýÈç:Class.forName("java.util.Date")
ClassÀàµÄÊôÐԺͷ½·¨Çë²Î¼ûjdk
·´Éä
·´Éä¾ÍÊÇ°ÑJavaÀàÖеĸ÷ÖֳɷÖÓ³Éä³ÉÏàÓ¦µÄjavaÀà¡£ÀýÈ磺һ¸öJavaÀàÖÐÓÃÒ»¸öClassÀàµÄ¶ÔÏóÀ´±íʾ£¬Ò»¸öÀàÖеÄ×é³É²¿·Ö£º³ÉÔ±±äÁ¿£¬·½·¨£¬¹¹Ôì·½·¨£¬°üµÈµÈÐÅÏ¢Ò²ÓÃÒ»¸ö¸öµÄJavaÀàÀ´±íʾ¡£±íʾjavaÀàµÄClassÀàÏÔȻҪÌṩһϵÁеķ½·¨À´»ñµÃÆäÖеıäÁ¿£¬·½·¨£¬¹¹Ôì·½·¨£¬ÐÞÊηû£¬°üµÈÐÅÏ¢£¬ÕâЩÐÅÏ¢¾ÍÊÇÓÃÏàÓ¦ÀàµÄʵÀý¶ÔÏóÀ´±íʾ£¬ËüÃÇÊÇField¡¢Method¡¢Contructor¡¢PackageµÈµÈ¡£
ContructorÀà
1¡¢µÃµ½Ä³¸öÀàµÄËùÓй¹Ôì·½·¨£º
Contructor[] contructors = lass.forName("ˈ̞").getContructors();
2¡¢µÃµ½Ä³¸öÌض¨µÄ¹¹Ôì·½·¨£º
//»ñµÃ·½·¨Ê±ÒªÓõ½ÀàÐÍ
Contructor contructors = Class.forName("ÀàÃû").getContructor(²ÎÊýÀàÐÍ.class,...);
3¡¢´´½¨ÊµÀý¶ÔÏó
ͨ³£·½Ê½:String str = new String(new StringBuffer("abc"));
//µ÷ÓûñµÃµÄ·½·¨Ê±ÒªÓõ½ÉÏÃæÏàͬÀàÐ͵ÄʵÀý
·´É䷽ʽ:String str = (String)contructor.newInstance(new StringBuffer("abc"));
4¡¢Class.forName();
·Ê¾Àý:String obj = (String)Class.forName("java.lang.Str
Ïà¹ØÎĵµ£º
package Demo;
// ʹÓ÷ºÐÍͳһ´«ÈëµÄ²ÎÊýÀàÐÍ
class Info28<T> {
private T var; // ´ËÀàÐÍÓÉÍⲿ¾ö¶¨
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
public String toString() { // ¸²Ð´ObjectÀàÖеÄtoString()·½·¨
return this.var.toString();
......
package Demo;
// Java·ºÐÍÎÞ·¨ÏòÉÏתÐÍ
class Info23<T> {
private T var; // ¶¨Òå·ºÐͱäÁ¿
public void setVar(T var) {
this.var = var;
}
public T getVar() {
return this.var;
}
public String toString() { // Ö±½Ó´òÓ¡
return this.var.toString();
}
}
public class GenericsDem ......
ÀàÐÍת»» Îı¾¿òStringתΪdouble Double·â×°Àà
Òì³£´¦Àí
ÏÔʾ¸ñʽ
ÕûÊý½á¹ûĩβȥ.0
Ê×λ²»ÄÜΪ0
+/- ÎÊÌâ Êý´óÓÐbug
Á¬¼ÓÁ¬¼õÎÊÌâ
»ØÍË
³ý0±¨´í
²»ÄܳöÏÖ¶à¸öµã °´ÁËÒ»¸öµãÔÙ°´Ã»ÓÐ
×Ö·û´®ÊDz ......
ÀàµÄ³õʼ»¯ºÍ¶ÔÏó³õʼ»¯ÊÇ JVM ¹ÜÀíµÄÀàÐÍÉúÃüÖÜÆÚÖзdz£ÖØÒªµÄÁ½¸ö»·½Ú£¬Google ÁËÒ»±éÍøÂ磬ÓйØÀà×°ÔØ»úÖƵÄÎÄÕµ¹ÊDz»ÉÙ£¬È»¶øÀà³õʼ»¯ºÍ¶ÔÏó³õʼ»¯µÄÎÄÕ²¢²»¶à£¬ÌرðÊÇ´Ó×Ö½ÚÂëºÍ JVM ²ã´ÎÀ´·ÖÎöµÄÎÄÕ¸üÊÇÏÊÓÐËù¼û¡£
±¾ÎÄÖ÷Òª¶ÔÀàºÍ¶ÔÏó³õʼ»¯È«¹ý³Ì½øÐзÖÎö£¬Í¨¹ýÒ»¸öʵ¼ÊÎÊÌâÒýÈ룬½«Ô´´úÂëת»»³É JVM ×Ö½ÚÂëºó£¬ ......
×ܽá¹Ø¼ü×ÖfinalµÄÓ÷¨¡£
author: ZJ 07-3-16
Blog: [url]http://zhangjunhd.blog.51cto.com/[/url]
ÔÚJavaÖÐÉùÃ÷ÊôÐÔ¡¢·½·¨ºÍÀàʱ£¬¿ÉʹÓùؼü×ÖfinalÀ´ÐÞÊΡ£final±äÁ¿¼´Îª³£Á¿£¬Ö»Äܸ³ÖµÒ»´Î£»final·½·¨²»Äܱ»×ÓÀàÖØд£»finalÀ಻Äܱ»¼Ì³Ð¡£
1£®final³ÉÔ±
ÉùÃ÷ final ×Ö¶ÎÓÐÖúÓÚÓÅ»¯Æ÷×÷³ö¸üºÃµÄÓÅ»¯¾ö¶¨£¬ÒòΪÈç¹û ......