慕运维8079593
按照一楼的使用TypesuperClass=getClass().getGenericSuperclass();但同时你测试的时候需要使用匿名内部类的实现,注意MyDemo最后的{},使MyDemo是Demo$0匿名的,使其父类是Demo,而不是Demo。注:这个测试最终什么都打印不出来,因为String.class.newInstance()是空字符串publicclassDemo{privateClassclazz;publicTgetDemo()throwsInstantiationException,IllegalAccessException{TypesuperClass=getClass().getGenericSuperclass();if(superClassinstanceofParameterizedType){Typetype=((ParameterizedType)superClass).getActualTypeArguments()[0];this.clazz=(Class)type;}else{System.out.println("不相等");}returnclazz.newInstance();}publicstaticvoidmain(String[]args)throwsIllegalAccessException,InstantiationException{DemoMyDemo=newDemo(){};Stringstr=MyDemo.getDemo();System.out.println(str);}}