简单了解class类

简单了解class类方法名 实例类 getClass 方法参数 null 返回类型 Class class 类 div

大家好,欢迎来到IT知识分享网。

一、链接class类5种方式

1、实例类.getClass()

 c.explain(1, "实例类.getClass()", null, "Class<?>", "1、通过创建实例对象,获取当前对象的class对象"); System.out.println("\t\t"+t.getClass()); System.out.println("\n\t-------------------------------------------------");

 2、Class.forName(String className)

c.explain(2, "Class.forName(String className)", "String className", " Class<?>", "1、这是采用Class类的静态方法,\n\t\t\t" + "2、根据类名获取class对象,\n\t\t\t" + "3、需要try捕捉异常。\n\t\t\t" + "4、className 包名.类名,如果在src文件夹里创建的类,可以省略包名"); try { System.out.println("\t\t"+Class.forName("class_students.TReflect")); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

3、类名.class

 c.explain(3, "类对象.class", null, "Class<?>", "1、根据类名获取class对象。" ); System.out.println("\t\t"+TReflect.class); System.out.println("\n\t-------------------------------------------------");

4、类对象.ClassLoader().loadClass(指定包名.类)

 c.explain(4, "类对象.ClassLoader().loadClass(指定包名.类)", "String className 指定类的包名.类", "Class<?>", "1、通过ClassLoader的loadClass方法,\n\t\t\t"+ "2、指定类,其他类在同包内,可以省略其他类的包名,\n\t\t\t"+ "3、如果在src文件夹里创建的类,可以省略指定类包名,\n\t\t\t"+ "4、如果其他类在其他包里,则需要用Class.forName(其他类)来加载指定类."); try { System.out.println("\t\t1、其他类和指定类不在同包里,Class.forName(\"TestReflect\") 例子:"); ClassLoader g = Class.forName("TestReflect").getClassLoader(); System.out.println("\t\t"+g.loadClass("class_students.TReflect")); System.out.println("\n\t\t2、其他类和指定类在同包里,Class_stu.class 例子:"); ClassLoader g1 = Class_stu.class.getClassLoader(); System.out.println("\t\t"+g.loadClass("class_students.TReflect")); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

5、基本数据类型的包装类.TYPE

c.explain(5, "基本数据类型的包装类.TYPE", null, " Class<Integer>", "1、获取特殊class类对象,基本数据类型的包装类都有一个TYPE属性。"); System.out.println("\t\t"+Integer.TYPE); System.out.println("\n\t===============================================\n");

二、class类方法的使用

1、public T newInstance()

c.explain(1, "public T newInstance() " + "throws InstantiationException, IllegalAccessException", null, "T", "1、需要try捕捉异常,\n\t\t\t" + "2、通过类对象创建类对象的实例。\n\t\t\t" + "3、如果不是TReflect.class.newInstance(),则需要强制声明强制转换关系" ); try { TReflect newInstance=TReflect.class.newInstance(); System.out.println("\t\t"+TReflect.class.newInstance().toString()); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

 2、public String toString()

 c.explain(2, "public String toString()", null, " String", "1、获取类对象的字符串表示。"); System.out.println("\t\t"+t.getClass().toString()); System.out.println("\n\t-------------------------------------------------");

3、public String toGenericString()

 c.explain(3, "public String toGenericString()", null, " String", "1、获取类对象的字符串表示,\n\t\t\t"+ "2、可以清楚知道这个类对象的修饰符和包名。");; System.out.println("\t\t"+tC.toGenericString()); System.out.println("\n\t-------------------------------------------------");

4、public String getSimpleName()

 c.explain(4, "public String getSimpleName()", null, " String", "1、获取类对象的简单名称,\n\t\t\t"+ "2、类是匿名的,则空。"); //创建匿名类实例对象 OutClass d = new OutClass(){ public void toOutClassName(){ System.out.println("OutClass"); } }; System.out.println("\t\t1、"+d.getClass()+"匿名类 例子:"+d.getClass().getSimpleName()); System.out.println("\t\t2、"+ d.getClass()+"例子:"+tC.getSimpleName()); System.out.println("\n\t-------------------------------------------------");

5、public String getName()

 c.explain(5, "public String getName()", null, " String", "1、获取类对象的名称。"); System.out.println("\t\t"+tC.getName()); System.out.println("\n\t-------------------------------------------------"); 

6、public String getCanonicalName()

 c.explain(6, "public String getCanonicalName()", null, " String", "1、获取类对象的规范名称。"); System.out.println("\t\t"+tC.getCanonicalName()); System.out.println("\n\t-------------------------------------------------");

7、public String getTypeName()

 c.explain(7, "public String getTypeName()", null, " String", "1、获取类对象的类型名称。"); System.out.println("\t\t"+tC.getTypeName()); System.out.println("\n\t-------------------------------------------------");

8、public Package getPackage()

 c.explain(8, "public Package getPackage()", null, " Package", "1、获取类对象的包对象。"); System.out.println("\t\t"+tC.getPackage()); System.out.println("\n\t-------------------------------------------------");

9、public Class<?>[] getInterfaces()

 c.explain(9, "public Class<?>[] getInterfaces()", null, " Class<?>[]", "1、获取类对象的接口对象的数组。"); for(Class<?> i : tC.getInterfaces()){ System.out.println("\t\t"+i.getName()); } System.out.println("\n\t-------------------------------------------------");

10、public Class<? super T> getSuperclass()

 c.explain(10, "public Class<? super T> getSuperclass()", null, " Class<?>", "1、获取类对象的父类对象。" + "2、如果没有父类对象,则为Object对象"); System.out.println("\t\t"+tC.getSuperclass()); System.out.println("\n\t-------------------------------------------------");

11、public int getModifiers()

 c.explain(11, "public int getModifiers()", null, " int", "1、获取类对象的修饰符。"); System.out.println("\t\t"+tC.getName()+"的修饰符:"+tC.getModifiers()); System.out.println("\t\t"+"Modifier.isPublic(tC.getModifiers()) 结果:"+Modifier.isPublic(tC.getModifiers())); System.out.println("\n\t-------------------------------------------------"); 

12、public boolean isInterface()

 c.explain(12, "public boolean isInterface()", null, " boolean", "1、判断类对象是否是public接口类。"); System.out.println("\t\t"+Integer.class.getName()+"结果:"+String.class.isInterface()); System.out.println("\t\t"+Comparable.class.getName()+"结果:"+Comparable.class.isInterface()); System.out.println("\n\t-------------------------------------------------");

13、public reflect.Type[] getGenericInterfaces()

c.explain(13, "public reflect.Type[] getGenericInterfaces()", null, " reflect.Type[]", "1、获取类对象的泛型接口对象数组。\n\t\t\t" + "2、如果此类不实现任何接口,则返回一个长度为 0 的数组。"); Type[] genericInterfaces = tC.getGenericInterfaces(); for (Type type : genericInterfaces) { if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; System.out.println("\t\t"+"Interface: " + pType.getRawType()); // 输出接口名 System.out.println("\t\t"+"Type arguments: " + Arrays.toString(pType.getActualTypeArguments())); // 输出泛型参数类型 } else { System.out.println("\t\t"+"Non-generic interface: " + type); } } System.out.println("\n\t-------------------------------------------------");

14、public reflect.Type getGenericSuperclass()

 c.explain(14, "public reflect.Type getGenericSuperclass()", null, " reflect.Type", "1、获取类对象的泛型父类对象。\n\t\t\t" + "2、如果此类没有父类,则返回 null。"); System.out.println("\t\t"+tC.getGenericSuperclass()); System.out.println("\n\t-------------------------------------------------");

15、public reflect.TypeVariable<Class<T>>[] getTypeParameters()

c.explain(15, "public reflect.TypeVariable<Class<T>>[] getTypeParameters()", null, " reflect.TypeVariable<Class<T>>[]", "1、获取类对象的泛型参数对象数组。\n\t\t\t" + "2、如果此类没有泛型参数,则返回一个长度为 0 的数组。"); OutClass1<String, Integer> outClass1 = new OutClass1<String,Integer>(); System.out.println("\t\t"+outClass1.getClass().getName()+"的泛型参数:"+ Arrays.toString(outClass1.getClass().getTypeParameters())); System.out.println("\n\t-------------------------------------------------");

16、public Class<?>[] getClasses()

 c.explain(16, "public Class<?>[] getClasses()", null, " Class<?>[]", "1、获取类对象的public内部类对象数组。\n\t\t\t" + "2、如果此类没有内部类,则返回一个长度为 0 的数组。"); for(Class i :Class_stuOut2.class.getClasses()){ System.out.println("\t\t"+i.getName()); } System.out.println("\n\t-------------------------------------------------");

17、public Class<?>[] getDeclaredClasses()

 c.explain(17, "public Class<?>[] getDeclaredClasses() " + "throws SecurityException", null, " Class<?>[]", "1、获取类对象的所有内部类对象数组。\n\t\t\t" + "2、如果此类没有内部类,则返回一个长度为 0 的数组。"); for(Class i :Class_stuOut2.class.getDeclaredClasses()){ System.out.println("\t\t"+i.getName()); } System.out.println("\n\t-------------------------------------------------");

18、public java.security.ProtectionDomain getProtectionDomain()

c.explain(18, "public java.security.ProtectionDomain getProtectionDomain()", null, " java.security.ProtectionDomain", "1、获取类对象的保护域,\n\t\t\t" + "2、如果存在安全管理器,并且其 checkPermission 方法不允许获取 ProtectionDomain。,\n\t\t\t" + "3、信息太多了,用getPrincipals()隐藏了信息"); System.out.println("\t\t"+tC.getProtectionDomain().getPrincipals()); System.out.println("\n\t-------------------------------------------------");

19、public java.io.InputStream getResourceAsStream(String name )

 c.explain(19, "public java.io.InputStream getResourceAsStream(String name )", " String name", " java.io.InputStream", "1、由这个类对象作为类加载指定的资源,资源须在SRC包里。\n\t\t\t" + "2、如果此类没有指定名称的资源,则返回 null。"); try { InputStream is=tC.getResourceAsStream("../srctest.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); while (true) { String line = reader.readLine(); if (line == null) { break; } System.out.println("\t\t"+line); } }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

20、public java.net.URL getResource(String name )

c.explain(20, "public java.net.URL getResource(String name )", " String name", " java.net.URL", "1、由这个类对象作为类加载指定的资源,资源须在SRC包里。\n\t\t\t" + "2、如果此类没有指定名称的资源,则返回 null。"); try { URL url = tC.getResource("../srctest.txt"); //(InputStream) url.getContent() 这种也可以 BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); while (true) { String line = reader.readLine(); if (line == null) { break; } System.out.println("\t\t"+line); } }catch(Exception e){} System.out.println("\n\t-------------------------------------------------");

21、public boolean desiredAssertionStatus()

 c.explain(21, "public boolean desiredAssertionStatus()", null, " boolean", "1、获取类对象的断言状态。\n\t\t\t" + "2、如果此类没有设置断言状态,则返回 false。" + "3、下面列子利用加载类设置指定类的断言状态"); try { Class asaertTest = Class_stuOut.class; tC.getClassLoader().setClassAssertionStatus("class_students.Class_stuOut", true); System.out.println("\t\t"+asaertTest.getName()+"的断言状态:"+asaertTest.desiredAssertionStatus()); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

22、public T[] getEnumConstants()

c.explain( 22, "public T[] getEnumConstants()", null, " T[]", "1、获取类对象的枚举常量数组。\n\t\t\t" + "2、如果此类没有枚举常量,则返回一个长度为 0 的数组。" ); String seasonName="" ; for (Class_stuSeason s : Class_stuSeason.class.getEnumConstants()){ seasonName = s.seasonName +"季节、"+seasonName; } seasonName=seasonName.substring(0, seasonName.length()-1); System.out.println("\t\t"+Class_stuSeason.class.getName()+"的枚举常量:"+seasonName); System.out.println("\n\t-------------------------------------------------"); 

23、public Class<?> getEnclosingClass() throws SecurityException

c.explain( 23, "public Class<?> getEnclosingClass() " + "throws SecurityException", null, " Class<?>", "1、获取类对象的外部类对象(也叫封闭类)。\n\t\t\t" + "2、如果此类没有外部类,则返回 null。" ); System.out.println("\t\t"+Class_stuOut.OutInnerClass.class.getEnclosingClass()); System.out.println("\n\t-------------------------------------------------"); 

24、public reflect.Method getEnclosingMethod()

 c.explain( 24, "public reflect.Method getEnclosingMethod() " + "throws SecurityException", null, "Class<?>", "1、获取类对象的外部方法对象(也叫封闭方法)。\n\t\t\t" + "2、如果此类没有外部方法,则返回 null。" + "3、下面例子现在外部类调用方法,让外部class成员获取方法里面的类。" ); Class_stuOut.getEncMethod(); Class MethodinnerClass = Class_stuOut.obj_class; System.out.println("\t\t"+MethodinnerClass.getName()+"的外部方法内类调用.getEnclosingMethod()" + "\n\t\t结果:"+MethodinnerClass.getEnclosingMethod()); System.out.println("\n\t-------------------------------------------------");

25、public reflect.Constructor<?> getEnclosingConstructor() throws SecurityException

 c.explain( 25, "public reflect.Constructor<?> getEnclosingConstructor() " + "throws SecurityException", null, "reflect.Constructor<?>", "1、获取类对象的外部构造方法对象(也叫封闭构造方法)。\n\t\t\t" + "2、如果此类没有外部构造方法,则返回 null。" + "3、下面例子现在外部类调用构造方法,让外部class成员获取方法里面的类。"); Class_stuOut cECon = new Class_stuOut("构造类调用"); Class cecclass = cECon.obj_class_Constr; System.out.println("\t\t"+cecclass.getName()+"的外部构造方法内类调用.getEnclosingClass()" + "\n\t\t结果:"+cecclass.getEnclosingConstructor()); System.out.println("\n\t-------------------------------------------------"); 

26、public ClassLoader getClassLoader()

 c.explain( 26, "public ClassLoader getClassLoader()", null, "ClassLoader", "1、获取类对象的ClassLoader对象。" ); System.out.println("\t\t"+tC.getName()+"的ClassLoader对象 :"+tC.getClassLoader()); System.out.println("\n\t-------------------------------------------------"); 

27、public <U> Class<? extends U> asSubclass( Class<U> clazz )

        Class dd =son1.asSubclass(father1) 强制申明了dd是TReflectParentClass的子类,

c.explain( 27, "public <U> Class<? extends U> asSubclass( Class<U> clazz )", "Class<U> clazz", "<U> Class<? extends U>", "1、将此类对象转换为指定类的子类,即强制换声明关系,强制说明了这个类是指定类的子类。\n\t\t\t" + "2、如果此类对象不是指定类的子类,则抛出 ClassCastException。\n\t\t\t" ); Class father1 = TReflectParentClass.class; Class son1 =TReflect.class; Class dd =son1.asSubclass(father1); try { TReflect ef = (TReflect)dd.newInstance(); System.out.println("\t\tClass dd =son1.asSubclass(father1) 强制申明了dd是TReflectParentClass的子类," ); }catch(Exception e){}; System.out.println("\n\t-------------------------------------------------");

28、public Class<?> getComponentType()

c.explain( 28, "public Class<?> getComponentType()", null, "Class<?>", "1、获取数组的元素类型。\n\t\t\t" + "2、如果此类不是数组,则抛出 UnsupportedOperationException。\n\t\t\t" + "3、下面例子获取数组的元素类型。" ); System.out.println("\t\t"+String[].class.getName()+"结果: "+String[].class.getComponentType()); System.out.println("\t\t"+String.class.getName()+"结果: "+String.class.getComponentType()); System.out.println("\n\t-------------------------------------------------");

29、public T cast()

 c.explain( 29, "public T cast()", null, "T", "1、将此类对象转换为指定的类型。\n\t\t\t" + "2、如果此类对象不是指定的类型,则抛出 ClassCastException。\n\t\t\t" + "3、下面例子将类转换为父类,让父类调用子类的构造方法。" ); Class_stuOutSon castFatherTest = new Class_stuOutSon(); Class_stuOutFather cft = (Class_stuOutFather)outSon.cast(castFatherTest); System.out.println("\t\t强制转化为父类,但cft.class是子类的class 结果:"+cft.getClass().getName()); System.out.println("\n\t-------------------------------------------------"); 

 30、public reflect.Field getField(String name) throws NoSuchFieldException, SecurityException

 c.explain( 30, "public reflect.Field getField(String name) " + "throws NoSuchFieldException, SecurityException", "String name", "reflect.Field", "1、获取指定名称的public字段或父字段。\n\t\t\t" + "2、如果此类没有指定名称的字段,则抛出 NoSuchFieldException。\n\t\t\t" + "3、下面例子获取字段。" ); try { Field c_a = c.getClass().getField("Class_stu_i"); System.out.println("\t\t"+c.getClass().getName()+"的字段:"+c_a.getName()); Field son_fa = outSon.getField("father_i"); System.out.println("\t\t"+outSon.getName()+"的父字段:"+son_fa.getName()); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------"); 

31、public reflect.Field[] getFields() throws SecurityException

 c.explain( 31, "public reflect.Field[] getFields() " + "throws SecurityException", null, "reflect.Field[]", "1、获取此类所有public字段及父类public字段。\n\t\t\t" + "2、如果此类没有public字段,则返回一个长度为 0 的数组。\n\t\t\t" + "3、下面例子获取字段。" ); for(Field f:outSon.getFields()){ System.out.println("\t\t"+outSon.getName()+"的字段 :"+f.getName()); } System.out.println("\n\t-------------------------------------------------"); 

32、public reflect.Field[] getDeclaredFields() throws SecurityException

c.explain( 32, "public reflect.Field[] getDeclaredFields() throws SecurityException", null, "reflect.Field[]", "1、获取此类所有字段。\n\t\t\t" + "2、如果此类没有字段,则返回一个长度为 0 的数组。\n\t\t\t" + "3、下面例子获取字段。" ); Class_stuOutSon dso = new Class_stuOutSon(); for(Field f:outSon.getDeclaredFields()){ try { System.out.println("\t\t"+outSon.getName()+"的字段 :"+f.getName()+"字段类型: "+f.getType().getName() + "\n\t\t字段修饰符: "+Modifier.toString(f.getModifiers())); System.out.println("\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); }catch (Exception e) { }} System.out.println("\n\t-------------------------------------------------");

33、public reflect.Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException

 c.explain( 33, "public reflect.Field getDeclaredField(String name) " + "throws NoSuchFieldException, SecurityException", "String name", "reflect.Field", "1、获取指定此类名称的字段。\n\t\t\t" + "2、如果此类没有指定名称的字段,则抛出 NoSuchFieldException。\n\t\t\t" + "3、下面例子获取字段。" ); try { Field dso_f = outSon.getDeclaredField("son_i"); System.out.println("\t\t"+outSon.getName()+"的字段:"+dso_f.getName()); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

34、public reflect.Constructor<T> getConstructor(Class<?>… parameterTypes) throws NoSuchMethodException, SecurityException

 c.explain( 34, "public reflect.Constructor<T> getConstructor(Class<?>... parameterTypes) \n\t\t" + "throws NoSuchMethodException, SecurityException", "Class<?>... parameterTypes", "reflect.Constructor<T>", "1、获取指定参数类型的构造方法。\n\t\t\t" + "2、如果此类没有指定参数类型的构造方法,则抛出 NoSuchMethodException。"); try { Class_stuOutSon conc = new Class_stuOutSon(2); Constructor con_c = outSon.getConstructor(int.class); System.out.println("\t\t"+outSon.getName()+"的构造方法:"+con_c); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

35、public reflect.Constructor<?>[] getConstructors() throws SecurityException

 c.explain( 35, "public reflect.Constructor<?>[] getConstructors() " + "throws SecurityException", null, "reflect.Constructor<?>[]", "1、获取public类型的构造方法。\n\t\t\t" + "2、如果此类没有构造方法,则抛出 NoSuchMethodException。"); for(Constructor con:outSon.getConstructors()){ System.out.println("\t\t"+outSon.getName()+"的构造方法:"+con); } System.out.println("\n\t-------------------------------------------------");

36、public reflect.Constructor<?>[] getDeclaredConstructors() throws SecurityException

c.explain( 36, "public reflect.Constructor<?>[] getDeclaredConstructors() " + "throws SecurityException", null, "reflect.Constructor<?>[]", "1、获取所有构造方法。\n\t\t\t" + "2、如果此类没有构造方法,则抛出 NoSuchMethodException。"); for(Constructor con:outSon.getDeclaredConstructors()){ System.out.println("\t\t"+outSon.getName()+"的构造方法:"+con); } System.out.println("\n\t-------------------------------------------------"); 

37、public reflect.Constructor<T> getDeclaredConstructor(Class<?>… parameterTypes) throws NoSuchMethodException, SecurityException

 c.explain( 37, "public reflect.Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) \n\t\t" + "throws NoSuchMethodException, SecurityException", "Class<?>... parameterTypes", "reflect.Constructor<T>", "1、获取指定参数类型的构造方法。\n\t\t\t" + "2、如果此类没有指定参数类型的构造方法,则抛出 NoSuchMethodException。"); try { Constructor con_c = outSon.getDeclaredConstructor(); System.out.println("\t\t"+outSon.getName()+"的构造方法:"+con_c); }catch (Exception e) {} System.out.println("\n\t-------------------------------------------------");

38、public reflect.Method getMethod(String name, Class<?>… parameterTypes) throws NoSuchMethodException, SecurityException

 c.explain( 38, "public reflect.Method getMethod(String name, Class<?>... parameterTypes)\n\t\t " + "throws NoSuchMethodException, SecurityException", "String name, Class<?>... parameterTypes", "reflect.Method", "1、获取指定public名称和参数类型的方法。\n\t\t\t" + "2、如果此类没有指定名称和参数类型的方法,则抛出 NoSuchMethodException。"); try { Method m = outSon.getMethod("Class_stuOutSonMethod"); System.out.println("\t\t"+outSon.getName()+"的构造方法:"+m); }catch(Exception e){} System.out.println("\n\t-------------------------------------------------");

39、public reflect.Method[] getMethods() throws SecurityException

c.explain( 39, "public reflect.Method[] getMethods() " + "throws SecurityException", null, " reflect.Method[]", "1、获取所有public的此类方法和父方法。\n\t\t\t" + "2、如果没有此类方法和父方法,则抛出 NoSuchMethodException。"); for(Method m:outSon.getMethods()){ System.out.println("\t\t"+outSon.getName()+"的构造方法:"+m); } System.out.println("\n\t-------------------------------------------------");

40、public reflect.Method[] getDeclaredMethods() throws SecurityException

c.explain( 40, "public reflect.Method[] getDeclaredMethods() " + "throws SecurityException", null, "reflect.Method[]", "1、获取此类所有方法。\n\t\t\t" + "2、如果此类没有方法,则抛出 NoSuchMethodException。"); for(Method m:outSon.getDeclaredMethods()){ System.out.println("\t\t"+outSon.getName()+"的构造方法:"+m); } System.out.println("\n\t-------------------------------------------------"); 

41、public reflect.Method getDeclaredMethod(String name,Class<?>… parameterTypes ) throws NoSuchMethodException, SecurityException

c.explain( 41, "public reflect.Method getDeclaredMethod(String name,Class<?>... parameterTypes )\n\t\t" + "throws NoSuchMethodException, SecurityException", " String name,Class<?>... parameterTypes", "reflect.Method", "1、获取指定名称和参数类型的此类方法。\n\t\t\t" + "2、如果此类没有指定名称和参数类型的此类方法,则抛出 NoSuchMethodException。" ); try { Method m = outSon.getDeclaredMethod("Class_stuOutSonMethod"); System.out.println("\t\t"+outSon.getName()+"的构造方法:"+m); }catch (Exception e){} System.out.println("\n\t-------------------------------------------------");

42、public <A extends annotation.Annotation> A getAnnotation(Class<A> annotationClass)

 c.explain( 42, "public <A extends annotation.Annotation> A getAnnotation(Class<A> annotationClass)", "Class<A> annotationClass", "A", "1、获取此类指定类型的public类注解。\n\t\t\t" + "2、如果此类没有指定类型的类注解,则返回 null。\n\t\t\t" + "3、如果此类有多个指定类型的类注解,则返回此类中最近的那个。\n\t\t\t" + "4、如果此类有多个指定类型的类注解,并且它们都没有指定元注解,则抛出 IllegalArgumentException。"); System.out.println("\t\t"+outSon.getName()+"的指定注解信息 :"+ outSon.getAnnotation( Author.class) ); System.out.println("\n\t-------------------------------------------------"); 

43、public annotation.Annotation[] getAnnotations()

c.explain( 43, "public annotation.Annotation[] getAnnotations()", null, "annotation.Annotation[]", "1、获取此类所有public类注解。"); for(Annotation a:outSon.getAnnotations()){ System.out.println("\t\t"+outSon.getName()+"的类注解信息 :"+ a); } System.out.println("\n\t-------------------------------------------------");

 44、public annotation.Annotation[] getDeclaredAnnotations()

c.explain( 44, "public annotation.Annotation[] getDeclaredAnnotations()", null, "annotation.Annotation[]", "1、获取此类所有类注解。"); for(Annotation a:outSon.getDeclaredAnnotations()){ System.out.println("\t\t"+outSon.getName()+"的类注解信息 :"+ a); } System.out.println("\n\t-------------------------------------------------");

 45、public <A extends annotation.Annotation> A getDeclaredAnnotation( Class<A> annotationClass )

 c.explain( 45, "public <A extends annotation.Annotation> A " + "getDeclaredAnnotation( Class<A> annotationClass )", "Class<A> annotationClass", "A", "1、获取此类指定类型的类注解。\n\t\t\t" + "2、如果此类没有指定类型的类注解,则返回 null。\n\t\t\t" + "3、如果此类有多个指定类型的类注解,则返回此类中最近的那个。\n\t\t\t" + "4、如果此类有多个指定类型的类注解,并且它们都没有指定元注解,则抛出 IllegalArgumentException。"); System.out.println("\t\t"+outSon.getName()+"的指定注解信息 :"+ outSon.getDeclaredAnnotation( Author.class) ); System.out.println("\n\t-------------------------------------------------");

46、public reflect.AnnotatedType[] getAnnotatedInterfaces()

c.explain( 46, "public reflect.AnnotatedType[] getAnnotatedInterfaces()", null, "reflect.AnnotatedType[]", "1、获取此类所有public接口的AnnotatedType。\n\t\t\t" + "2、如果此类没有public接口,则返回一个长度为0的数组。\n\t\t\t" + "3、如果此类有多个public接口,则返回一个包含所有public接口的AnnotatedType的数组。\n\t\t\t"); for(AnnotatedType a:outSon.getAnnotatedInterfaces()){ System.out.println("\t\t"+outSon.getName()+"的接口信息 :"+ a.getType()); } System.out.println("\n\t-------------------------------------------------");

47、”public reflect.AnnotatedType getAnnotatedSuperclass()

c.explain( 47, "public reflect.AnnotatedType getAnnotatedSuperclass()", null, "reflect.AnnotatedType", "1、获取此类的AnnotatedType。\n\t\t\t" + "2、如果此类没有超类,则返回 null。\n\t\t\t" + "3、如果此类有多个超类,则返回此类中最近的那个。\n\t\t\t"); System.out.println("\t\t"+outSon.getName()+"的超类信息 :"+ outSon.getAnnotatedSuperclass().getType()); System.out.println("\n\t-------------------------------------------------");

48、public <A extends annotation.Annotation> A[] getAnnotationsByType(Class<A> annotationClass )

48、方法名:public <A extends annotation.Annotation> A[] getAnnotationsByType(Class<A> annotationClass ) 方法参数:Class<A> annotationClass 返回类型:A[] 方法说明:1、获取此类指定类型的public类注解。 2、如果此类没有指定类型的类注解,则返回一个长度为0的数组。 3、如果此类有多个指定类型的类注解,则返回一个包含所有指定类型的类注解的数组。 4、如果此类有多个指定类型的类注解,并且它们都没有指定元注解. 举例: class_students.Class_stuOutSon的类注解信息 :interface class_students.Target_TYPE_USE

49、public <A extends annotation.Annotation> A[] getDeclaredAnnotationsByType( Class<A> annotationClass )

49、方法名:public <A extends annotation.Annotation> A[] getDeclaredAnnotationsByType( Class<A> annotationClass ) 方法参数:Class<A> annotationClass 返回类型:A[] 方法说明:1、此类所有类型类注解都可以指定获取。 2、如果此类没有指定类型的类注解,则返回一个长度为0的数组。 3、如果此类有多个指定类型的类注解,则返回一个包含所有指定类型的类注解的数组。 举例: class_students.Class_stuOutSon的类注解信息 :interface class_students.Author

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/129671.html

(0)
上一篇 2025-08-22 18:10
下一篇 2025-08-22 18:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信