createbyname.java

来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 44 行

JAVA
44
字号
package examples.rtti;

/** A class used to demonstrate RTTI concepts
  */
public class CreateByName {

  /** Test method for the class
    * @param args names of classes to be
    *        instantiated
    */
   public static void main( String[] args ) {
      for ( int i = 0; i < args.length; i++ ) {
         try {
            Class x = Class.forName( args[i] );
            Object y;
         try {
            if ( x.isInterface() ) {
               System.out.println( "The class " + 
                  x.getName() + " is an interface "
                  + "and can't be instantiated." );
            } else {
               y = x.newInstance();
               if ( y instanceof java.awt.Component ) {
                  System.out.println( "The GUI "
                     + "component class "
                     + x.getName() + " was specified." );
               } else {
                  System.out.println("The non-GUI "
                     + "component class "
                     + x.getName() + " was specified." );
               }
            }
         } catch( InstantiationException ix ) {
            ix.printStackTrace();
         } catch( IllegalAccessException iax ) {
            iax.printStackTrace();
         } // end inner try
         } catch( ClassNotFoundException cnfx ) {
            System.err.println( "Sorry, the class "
               + args[i] + " could not be found." );
         } // end outer try
      } // end for
   } // end main
} // end CreateByName

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?