JAVA学习日记:反射机制(3)

    科技2022-07-20  113

    反射机制学习日记(3)

    今天是第三课:

    Method类中的方法的使用(含代码和注释):

    getMethods()获得本类及父类中的public权限修饰**符方法 getDeclaredMethods()专门获得调用该方法的对象的本类中的所有方法包括private权限修饰符**的方法 getDeclaredMethod(String name,class<?>...parameterTypes) 第一个参数:方法的名称 第二个参数:可变长度,写你要查找的那个方法的参数类型列表.class getParameterCount()得到方法的参数个数 package LessonForReflection03; import java.lang.reflect.Method; import java.lang.reflect.Modifier; abstract class Card { private void creatRandomNumbers(int count)//private关键字 { } public void getFullCardsNumbers(String[] random, String pre_numbers) { } public static void getUserInfor() { } public abstract void getUserInfor(String tel); public abstract void getUserInfor(int sal1, int sal2) throws ArrayIndexOutOfBoundsException,ArithmeticException; } public class MethodInforGetter { public static void main(String[] args) { Class<?> c1 = Card.class; System.out.println("-------------------------"); Method[] m1 = c1.getMethods();//getMethods()获得本类及父类中的public方法! for (Method m:m1) { System.out.println(m); } System.out.println("-------------------------"); Method[] m2 = c1.getDeclaredMethods();//getDeclaredMethods()专门获得本类中的所有方法包括private! for (Method m:m2) { System.out.println(m); } System.out.println("-------------------------"); /* * getDeclaredMethod(String name,class<?>...parameterTypes) * 第一个参数:方法的名称 * 第二个参数:可变长度,写你要查找的那个方法的参数类型列表 * * getParameterCount()得到方法的参数个数 */ try { Method m3 = c1.getDeclaredMethod("getUserInfor"); System.out.println(m3); //getParameterCount()方法,获得方法参数个数 System.out.println(m3.getParameterCount()); System.out.println(Modifier.toString(m3.getModifiers()));//获得方法修饰符 System.out.println(m3.getReturnType()); System.out.println("-------------------------"); Method m4 = c1.getDeclaredMethod("getUserInfor", int.class,int.class); //getExceptionTypes()可以获得初始化当前Method对象的给Class对象初始化的那个类的那个指定方法抛出的异常类型 Class<?>[] exception = m4.getExceptionTypes(); for (Class<?> e:exception) { System.out.println(e); } } catch (NoSuchMethodException | SecurityException e) { e.printStackTrace(); } } }

    Constructor类中的方法的使用(含代码和注释):

    java.lang.reflect.Constructor: Constructor[] getConstructor()获得本类里的public权限修饰符构造函数,不能获取父类的! Constructor[] getDeclaredConstructor()获得本类中的所以构造函数! Constructor<T> getConstructor(Class...parameterType)用参数决定获得本类中的某个的构造方法,只能获得public的 Constructor<T> getDeclaredConstructor(Class...parameterType)用参数决定获得本类中的某个构造方法 附: JDK8.0之后新增的类: Executable: 它是Method和Constructor的父类 常用方法: getParameter()获得类中方法参数 getExceptionTypes()获得类中某个方法抛出异常类型 getMoidfiers()获得方法权限修饰符 Parameter: 封装并代表了参数实例 package LessonForReflection03; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.lang.reflect.Parameter; /* * java.lang.reflect.Constructor * * Constructor[] getConstructor();获得本类里的public权限修饰符构造函数,不能获取父类的 * Constructor[] getDeclaredConstructor();得本类里的全部构造 * * Constructor<T> getConstructor(Class...parameterType);用参数决定获得哪个构造方法 * Constructor<T> getDeclaredConstructor(Class...parameterType); * */ public class ConstructorInforGetter { public static void main(String[] args) { System.out.println("获得Cricle本类里的public权限修饰符构造函数,不能获取父类的Constructor[] getConstructor()"); System.out.println("子类继承不了父类中的构造方法和private"); //Constructor[] getConstructor()获得Cricle本类里的public权限修饰符构造函数,不能获取父类的 //子类继承不了父类中的构造方法和private Class<Circle> c1 = Circle.class; Constructor<?>[] cons1 = c1.getConstructors(); for (Constructor<?> cons:cons1) { System.out.println(cons); //System.out.println(cons.getName()); } System.out.println("-----------------------"); System.out.println("方法获得本类中的所有构造函数getDeclaredConstructor()"); Constructor<?>[] cons2 = c1.getDeclaredConstructors(); for (Constructor<?> cons:cons2) { System.out.println(cons); } System.out.println("-----------------------"); try { System.out.println("方法用参数指定获得本类!构造方法,只能获取public的Constructor<T> getConstructor(Class...parameterType)"); Constructor<?> cons3 = c1.getConstructor(int.class); System.out.println(Modifier.toString(cons3.getModifiers())); System.out.println(cons3); System.out.println("-----------------------"); System.out.println("方法用参数指定获得本类!构造方法任何权限修饰符的都可以获得Constructor<T> getDeclaredConstructor(Class...parameterType)"); Constructor<?> cons4 = c1.getDeclaredConstructor(String.class); System.out.println(cons4); System.out.println("-----------------------"); /* * JDK8.0之后新增的类 * Executable: * 是Method和Constructor的父类 * 方法: * getParameter(); * getExceptionTypes(); * getModifiers(); * getTypeParameters(); * * Parameter: * 封装并代表了参数实例 */ System.out.println("获取类中方法的参数getParameters()"); Constructor<?> cons5 = c1.getDeclaredConstructor(int.class,String.class); Parameter[] p1 = cons5.getParameters(); for (Parameter p:p1) { System.out.println(p); } } catch (NoSuchMethodException | SecurityException e) { e.printStackTrace(); } } }

    代码中提到的Circle类和Shape类二者为继承关系:

    package LessonForReflection03; public class Circle extends Shape { private int r; private String color; public Circle(int r, String color) { super(); this.r = r; this.color = color; } public Circle(int r) { super(); this.r = r; } protected Circle(String color) { super(); this.color = color; } Circle() { super(); } } package LessonForReflection03; public class Shape { private int per; public Shape(int per) { super(); this.per = per; } public Shape() { super(); } } 部分文字来源于: 咕嘟咖啡杨海滨老师 — 《java编程语言高级特性》 轻量化研习Java相关技术倡导者 “爱码学院”联合创始人自适应教学理念提出者践行者;多年开发及项目管理经历;出版《JavaEE企业级应用与开发》一书;10余年高校项目实践毕设指导经验;企业软培经验丰富。

    可以转载我的学习日记但请注明出处,谢谢。

    Processed: 0.010, SQL: 8