目录
1.获取class 的三种方式
2.利用反射获取构造方法
3.利用反射获取成员变量
4.利用反射获取成员方法
1.获取class 的三种方式
全类名怎么找?
全类名=报名+类名
java">package MyReflect;public class Student {private String id;private String name;private int age;private String address;public Student(String id, String name, int age, String address) {this.id = id;this.name = name;this.age = age;this.address = address;}public Student() {}/*** 获取* @return id*/public String getId() {return id;}/*** 设置* @param id*/public void setId(String id) {this.id = id;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}/*** 获取* @return address*/public String getAddress() {return address;}/*** 设置* @param address*/public void setAddress(String address) {this.address = address;}public String toString() {return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}";}public String toMess(){return id+" "+name+" "+age+" "+address;}
}
java">package MyReflect;public class Test {public static void main(String[] args) throws ClassNotFoundException {//1 常用Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);//2 当作参数Class clazz2=Student.class;System.out.println(clazz2);System.out.println(clazz1 == clazz2);//3 当有对象的时候Student s1=new Student();Class clazz3=s1.getClass();System.out.println(clazz3);System.out.println(clazz1 == clazz3);}}
2.利用反射获取构造方法
java">package MyReflect;public class Student{private String name;private int age;public Student() {}protected Student(String name) {this.name = name;}protected Student(int age) {this.age = age;}private Student(String name, int age) {this.name = name;this.age = age;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student111{name = " + name + ", age = " + age + "}";}}
java">package MyReflect;import java.lang.reflect.Constructor;public class Test {public static void main(String[] args) throws ClassNotFoundException {//1.获取字节码Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);//2.获取构造方法Constructor[] constructor1= clazz1.getConstructors();for (Constructor constructor:constructor1) {System.out.println(constructor);}}}
java">package MyReflect;import java.lang.reflect.Constructor;public class Test {public static void main(String[] args) throws ClassNotFoundException {//1 常用Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);Constructor[]constructor1= clazz1.getDeclaredConstructors();for (Constructor constructor:constructor1) {System.out.println(constructor);}}}
java">package MyReflect;import java.lang.reflect.Constructor;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {//1 常用Class<?> clazz1= Class.forName("MyReflect.Student");System.out.println(clazz1);Constructor constructor1= clazz1.getDeclaredConstructor(String.class);System.out.println(constructor1);}}
3.利用反射获取成员变量
java">package MyReflect;import java.lang.reflect.Field;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Field[] fields=clazz.getDeclaredFields();for (Field field : fields) {System.out.println(field);}}}
java">package MyReflect;import java.lang.reflect.Field;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Field fields=clazz.getDeclaredField("name");//fields.setAccessible(true);System.out.println(fields);}}
对于private的变量,要
namefield.setAccessible(true);
java">package MyReflect;public class Student{private String name;private int age;public Student() {}protected Student(String name) {this.name = name;}protected Student(int age) {this.age = age;}public Student(String name, int age) {this.name = name;this.age = age;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student111{name = " + name + ", age = " + age + "}";}}
java">package MyReflect;import java.lang.reflect.Field;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Field namefield=clazz.getDeclaredField("name");System.out.println(namefield);Student student=new Student("leo",11);// namefield.setAccessible(true);Object value=namefield.get(student);System.out.println("name:"+value);}}
4.利用反射获取成员方法
java">package MyReflect;import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method[] methods=clazz.getDeclaredMethods();for (Method method : methods) {System.out.println(method);}}}
java">package MyReflect;import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method[] methods=clazz.getMethods();for (Method method : methods) {System.out.println(method);}}}
java">package MyReflect;import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException {//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method method=clazz.getMethod("getName");System.out.println(method);}}
java">package MyReflect;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {Student student = new Student("leo",11);//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method method=clazz.getDeclaredMethod("eat", String.class);System.out.println(method);method.invoke(student,"汉堡包");}}
java">package MyReflect;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.UnknownHostException;public class Test {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, UnknownHostException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {Student student = new Student("leo",11);//获取字节码文件Class clazz=Class.forName("MyReflect.Student");System.out.println("class:"+clazz);Method method=clazz.getDeclaredMethod("eat", String.class);method.setAccessible(true);System.out.println(method);method.invoke(student,"汉堡包");}}