关于反射的学习

it2022-05-05  139

程序入口:

import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Test { public static void main(String[] args) throws Exception { Class c = Test1.class; Test1 obj1 = (Test1)c.getConstructor().newInstance();//实例化该类 Method[] m = c.getDeclaredMethods();//找到方法并实例化 for (Method method : m) { System.out.println(method); }//打印全部的公有方法 Constructor[] con = c.getDeclaredConstructors(); //Object obj = c.newInstance(); for (Constructor co : con) { System.out.println(co); }//打印全部的构造方法 System.out.println("------------------------------------"); Field[] f = c.getDeclaredFields(); for (Field field : f) { System.out.println(field); }//打印全部属性 System.out.println("------------------------------------"); Field ff = c.getDeclaredField("name"); ff.setAccessible(true); ff.set(obj1,"战三");//给name赋值 System.out.println(obj1.getName());//获取name System.out.println("------------------------------------"); Method mm = c.getDeclaredMethod("add", String.class);//找到该方法并实例化该方法 mm.setAccessible(true);//暴力反射 mm.invoke(obj1,"小李");//传入参数 System.out.println("------------------------------------"); Method mmm = c.getMethod("find", String.class); mmm.invoke(obj1,"小明"); System.out.println("------------------------------------"); Constructor mmmm = c.getDeclaredConstructor(String.class,int.class); mmmm.setAccessible(true);//暴力反射 System.out.println(mmmm.newInstance("小李",20));//传入参数 } }

其他类: Test1

public class Test1 { private String name;//姓名 public int age;//年龄 protected String sex;//性别 int salary;//工资 private void add(String s){ System.out.println(s+"说这是私有增加方法"); } public void find(String s){ System.out.println(s+"说这是公有查询方法"); } protected void remove() { System.out.println("这是受保护的删除方法"); } void show(){ System.out.println("这是默认的显示方法"); } @Override public String toString() { return "姓名:" + name + ", 年龄:" + age + ", 性别:" + sex + ", 工资:" + salary ; } public Test1() { super(); } public Test1(String name, int age, String sex, int salary) { super(); this.name = name; this.age = age; this.sex = sex; this.salary = salary; } private Test1(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }

其他类:Dog

public class Dog { public String name;//姓名 public int age;//年龄 @Override public String toString() { return "Dog [name=" + name + ", age=" + age + "]"; } public Dog() { super(); } public Dog(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

其他类:Cat

public class Cat { public String name;//姓名 public int age;//年龄 @Override public String toString() { return "Cat [name=" + name + ", age=" + age + "]"; } public Cat() { super(); } public Cat(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

输出结果:


最新回复(0)