练习题1401
package basic; interface A1401{ } interface B1401{} interface C1401{} class Toy{ //public Toy(){ // } public Toy(int i){ } } class FancyToy extends Toy implements A1401,B1401,C1401{ public FancyToy(){ super(1); } } public class Test1401 { static void printInfo(Class cc){ System.out.println("Class name" + cc.getName() + "is interface ? [ "+ cc.isInterface() +"]" ); System.out.println("Class simplename :" + cc.getSimpleName()); System.out.println("Class canonical name :" + cc.getCanonicalName()); } public static void main(String[] args) { Class c = null; try { c = Class.forName("basic.FancyToy"); } catch (ClassNotFoundException e) { System.out.println("catch not find"); System.exit(1); } printInfo(c); for (Class inter: c.getInterfaces() ){ printInfo(inter); } Object obj = null; Class superclass = c.getSuperclass(); try { obj = superclass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } printInfo(obj.getClass()); } }
运行结果
Class namebasic.FancyToyis interface ? [ false] java.lang.InstantiationException: basic.Toy Class simplename :FancyToy at java.lang.Class.newInstance(Class.java:427) Class canonical name :basic.FancyToy Class namebasic.A1401is interface ? [ true] at basic.Test1401.main(Test1401.java:42) Class simplename :A1401 Class canonical name :basic.A1401 Caused by: java.lang.NoSuchMethodException: basic.Toy.<init>() Class namebasic.B1401is interface ? [ true] at java.lang.Class.getConstructor0(Class.java:3082) Class simplename :B1401 at java.lang.Class.newInstance(Class.java:412) Class canonical name :basic.B1401 ... 1 more Class namebasic.C1401is interface ? [ true] Class simplename :C1401 Exception in thread "main" java.lang.NullPointerException Class canonical name :basic.C1401 at basic.Test1401.main(Test1401.java:48) Process finished with exit code 1