Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

JavaÖÐnewºÍnewInstanceµÄÇø±ð

    ÔÚJavaÖУ¬Ê¹ÓÃnew¹Ø¼ü×ÖºÍClass.forName("a.class.Name").newInstance¶¼¿ÉÒÔ´´½¨¶ÔÏó£¬ËüÃÇÓÖÓÐʲôÇø±ðÄØ£¿
    1¡¢ÀàµÄ¼ÓÔØ·½Ê½²»Í¬¡£
    ÔÚÖ´ÐÐClass.forName("a.class.Name")ʱ£¬JVM»áÔÚclassapthÖÐÈ¥ÕÒ¶ÔÓ¦µÄÀಢ¼ÓÔØ£¬ÕâʱJVM»áÖ´ÐиÃÀàµÄ¾²Ì¬´úÂë¶Î¡£ÔÚʹÓÃnewInstance()·½·¨µÄʱºò£¬±ØÐë±£Ö¤Õâ¸öÀàÒѾ­¼ÓÔز¢ÇÒÒѾ­Á¬½ÓÁË£¬¶øÕâ¿ÉÒÔͨ¹ýClassµÄ¾²Ì¬·½·¨forName()À´Íê³ÉµÄ¡£
    ʹÓùؼü×Önew´´½¨Ò»¸öÀàµÄʱºò£¬Õâ¸öÀà¿ÉÒÔûÓб»¼ÓÔØ£¬Ò»°ãÒ²²»ÐèÒª¸ÃÀàÔÚclasspathÖÐÉ趨£¬µ«¿ÉÄÜÐèҪͨ¹ýclasslaoderÀ´¼ÓÔØ¡£
    2¡¢Ëùµ÷ÓõĹ¹Ôì·½·¨²»¾¡Ïàͬ¡£
    new¹Ø¼ü×ÖÄܵ÷ÓÃÈκι¹Ôì·½·¨¡£
    newInstance()Ö»Äܵ÷ÓÃÎ޲ι¹Ôì·½·¨¡£
    3¡¢Ö´ÐÐЧÂʲ»Í¬¡£
    new¹Ø¼ü×ÖÊÇÇ¿ÀàÐ͵ģ¬Ð§ÂÊÏà¶Ô½Ï¸ß¡£
    newInstance()ÊÇÈõÀàÐ͵ģ¬Ð§ÂÊÏà¶Ô½ÏµÍ¡£
    ¼ÈȻʹÓÃnewInstance()¹¹Ôì¶ÔÏóµÄµØ·½Í¨¹ýnew¹Ø¼ü×ÖÒ²¿ÉÒÔ´´½¨¶ÔÏó£¬ÎªÊ²Ã´ÓÖ»áʹÓÃnewInstance()À´´´½¨¶ÔÏóÄØ£¿
    ¼ÙÉ趨ÒåÁËÒ»¸ö½Ó¿ÚDoor£¬¿ªÊ¼µÄʱºòÊÇÓÃľÃŵģ¬¶¨ÒåΪһ¸öÀàWoodenDoor£¬ÔÚ³ÌÐòÀï¾ÍÒªÕâÑùд Door door = new WoodenDoor() ¡£¼ÙÉèºóÀ´Éú»îÌõ¼þÌá¸ß£¬»»Îª×Ô¶¯ÃÅÁË£¬¶¨ÒåÒ»¸öÀàAutoDoor£¬Õâʱ³ÌÐò¾ÍÒª¸ÄдΪ Door door = new AutoDoor() ¡£ËäȻֻÊǸĸö±êʶ·û£¬Èç¹ûÕâÑùµÄÓï¾äÌرð¶à£¬¸Ä¶¯»¹ÊÇͦ´óµÄ¡£ÓÚÊdzöÏÖÁ˹¤³§Ä£Ê½£¬ËùÓÐDoorµÄʵÀý¶¼ÓÉDoorFactoryÌṩ£¬Õâʱ»»Ò»ÖÖÃŵÄʱºò£¬Ö»ÐèÒª°Ñ¹¤³§µÄÉú²úģʽ¸Äһϣ¬»¹ÊÇÒª¸ÄÒ»µã´úÂë¡£
    ¶øÈç¹ûʹÓÃnewInstance()£¬Ôò¿ÉÒÔÔÚ²»¸Ä±ä´úÂëµÄÇé¿öÏ£¬»»ÎªÁíÍâÒ»ÖÖDoor¡£¾ßÌå·½·¨ÊÇ°ÑDoorµÄ¾ßÌåʵÏÖÀàµÄÀàÃû·Åµ½ÅäÖÃÎļþÖУ¬Í¨¹ýnewInstance()Éú³ÉʵÀý¡£ÕâÑù£¬¸Ä±äÁíÍâÒ»ÖÖDoorµÄʱºò£¬Ö»¸ÄÅäÖÃÎļþ¾Í¿ÉÒÔÁË¡£Ê¾Àý´úÂëÈçÏ£º
String className = ´ÓÅäÖÃÎļþ¶ÁÈ¡DoorµÄ¾ßÌåʵÏÖÀàµÄÀàÃû;
Door door = (Door) Class.forName(className).newInstance();
    ÔÙÅäºÏÒÀÀµ×¢ÈëµÄ·½·¨£¬¾ÍÌá¸ßÁËÈí¼þµÄ¿ÉÉìËõÐÔ¡¢¿ÉÀ©Õ¹ÐÔ¡£


Ïà¹ØÎĵµ£º

JAVA Ö®"·´Éä"


1¡¢ÀàÐÍÄ£°å Class£º
    ÎïÒÔÀà¾Û£º
    ÎÒÃÇ¿ÉÒÔÓÃclass Person{ }   class Animal{ }À´ÃèÊöÒ»ÀàÈË»òÒ»ÀදÎ
    ÄÇôÔÙÏëһϣ¬ÎÒÃÇÊDz»ÊÇ¿ÉÒÔÓÃclass Class{ } À´³éÏóÃèÊöÀàÄØ£¿
    ´ð°¸Ê±¿ÉÒԵģ¬Õâ¾ÍÊÇjdkÖÐµÄ Class ÀࣨÀàÐ ......

Java¶ÁÎļþдÎļþ²Ù×÷

  ÕâÀJavaµÄ¶ÁÎļþºÍдÎļþ¶¼ÊÇ»ùÓÚ×Ö·ûÁ÷µÄ£¬Ö÷ÒªÓõ½ÏÂÃæµÄ¼¸¸öÀࣺ
1¡¢FileReader----¶ÁÈ¡×Ö·ûÁ÷
2¡¢FileWriter----дÈë×Ö·ûÁ÷
3¡¢BufferedReader----»º³åÖ¸¶¨ÎļþµÄÊäÈë
¸ÃÀàµÄ·½·¨ÓУº
void close() 
        ¹Ø±Õ¸ÃÁ÷¡£
 void mark(int readAhead ......

JAVA »ñµÃIPµØÖ·

1. java »ñÈ¡ipµØÖ·µÄ´úÂë
    String loacalIp = "";
    try
   {
       InetAddress localhost = InetAddress.getLocalHost();
        loacalIp  =localhost.getHostAddress();
   ......

this ÓësuperÔÚjavaÖÐÔËÓÃÕûºÏ


 A.ʹÓùؼü×ÖthisµÄ×¼Ôò£º
1.ÔÚ¹¹ÔìÆ÷ÖУ¬Èç¹ûҪʹÓùؼü×Öthis,ÄÇô£¬±ØÐë·ÅÔÚµÚÒ»ÐУ¬·ñÔò½«µ¼ÖÂÒ»¸ö±àÒë´íÎó¡£
2.¹¹Ôì·½·¨ÒýÓÃthisÖ¸ÏòÕýÔÚÖ´Ðз½·¨µÄÀàµÄʵÀý¡£¾²Ì¬·½·¨²»ÄÜʹÓÃthis¹Ø¼ü×Ö£¬ÒòΪ¾²Ì¬·½·¨²»ÊôÓÚÀàµÄʵÀý£¬ËùÒÔthisÒ²¾ÍûÓÐʲô¶«Î÷È¥Ö¸Ïò¡£¹¹ÔìÆ÷µÄthisÖ¸Ïòͬһ¸öÀàÖУ¬²»Í¬²ÎÊýÁбíµÄÁíÍâÒ» ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ